Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: how to push new subdocument instance into existing Document array #61

Open
xinbenlv opened this issue Aug 21, 2020 · 0 comments

Comments

@xinbenlv
Copy link

xinbenlv commented Aug 21, 2020

Hi, ts-mongoose contributors, this is a great library.
My question is: is there an example of how to push new subdocument instance into existing Document array?

Thank you!

Here is our use case.

Here is what we try to setup

import { createSchema, ExtractDoc, ExtractProps, Type, typedModel } from 'ts-mongoose';

const NoticeAckSchema = createSchema(
  {
    wikiUserName: Type.string(),
    userGaId: Type.string(),
    ackedAt: Type.date(),
  },
);

/**
 * Schema for a NoticeMessage
 *
 * Example
 {
   messageId: "NoticeUSerLevelsRfC",
   defaultMessage: "Hi editors, we have a Request For Comments we like your feedback.",
   url: 'https://meta.wikimedia.org/wiki/WikiLoop/DoubleCheck/something',
   createdAt: "2020-08-20 01:01 UTC",
   asks: [
     {
        wikiUserName: "JohnSmith",
        userGaId: "...",
        ackedAt: "2020-08-20 02:02 UTC",
     }
   ],
   beginAt: "2020-08-20 00:00 UTC",
   endAt: "2020-09-14 00:00 UTC",
  }
 */
const NoticeMessageSchema = createSchema(
  {
    messageId: Type.string({ required: true, index: true, unique: true }),  // e.g.
    defaultMessage: Type.string({required: true}), // The default message content, in case translations are not currently available yet.
    url: Type.string({required: false}),
    createdAt: Type.date({required: true, index:true}),
    acks: Type.array().of(Type.schema({required:false}).of(NoticeAckSchema)),
    beginAt: Type.date({required: true, index:true}),
    endAt: Type.date({required: true, index:true}),
  },
);

export const NoticeMessage = typedModel('NoticeMessage', NoticeMessageSchema, 'NoticeMessage');
export type NoticeMessageDoc = ExtractDoc<typeof NoticeMessageSchema>;
export type NoticeMessageProps = ExtractProps<typeof NoticeMessageSchema>;
export type NoticeAckProps = ExtractProps<typeof NoticeAckSchema>;
export type NoticeAckDoc = ExtractDoc<typeof NoticeAckSchema>;

And here is an example code what we run

import { initDotEnv, initMongoDb } from '~/server/init-util';
import { NoticeMessage } from '~/shared/models/notice-message-model';

const createNoticeMain = async function () {
  await initDotEnv();
  await initMongoDb();

  let noticeMessage = await NoticeMessage.create({
    messageId: "Notice-UserLevelsRfC1",
    defaultMessage: "Hi editors, we have a Request For Comments we like your feedback.",
    url: 'https://meta.wikimedia.org/wiki/WikiLoop/DoubleCheck/RfC:Levels_for_WikiLoop_DoubleCheck_Reviewers',
    createdAt: new Date("2020-08-20 01:01 UTC"),
    acks: [
      {
        wikiUserName: "RandomUser",
        userGaId: "GA1.2.1390141177.1578026694", // random GA ID,
        ackedAt: new Date(),
      }
    ],
    beginAt: new Date("2020-08-20 00:00:00 UTC"),
    endAt: new Date("2020-09-14 00:00:00 UTC"),
  });
  noticeMessage.acks.push({
    wikiUserName: "RandomUser",
    userGaId: "GA1.2.1395341177.1578021394", // random GA ID,
    ackedAt: new Date(),
  });
  console.log(`done`);
}


createNoticeMain()
  .then(() => {
    console.log(`CMD Done!`);
    process.exit(0);
  });

And it seems failing at

  noticeMessage.acks.push({
    wikiUserName: "RandomUser",
    userGaId: "GA1.2.1395341177.1578021394", // random GA ID,
    ackedAt: new Date(),
  });

with error

 error TS2345: Argument of type '{ wikiUserName: string; userGaId: string; ackedAt: Date; }' is not assignable to parameter of type '{ get: (path: string, type?: any, options?: { virtuals?: boolean; getters?: boolean; }) => any; set: { (path: string, val: any, options?: any): { _id: ObjectId; __v: number; wikiUserName: string; userGaId: string; ackedAt: Date; } & {} & SubDocument; (path: string, val: any, type: any, options?: any): { ...; } & ......'.
  Type '{ wikiUserName: string; userGaId: string; ackedAt: Date; }' is missing the following properties from type '{ get: (path: string, type?: any, options?: { virtuals?: boolean; getters?: boolean; }) => any; set: { (path: string, val: any, options?: any): { _id: ObjectId; __v: number; wikiUserName: string; userGaId: string; ackedAt: Date; } & {} & SubDocument; (path: string, val: any, type: any, options?: any): { ...; } & ......': get, set, validate, _id, and 33 more.

 26   noticeMessage.acks.push({
                              ~
 27     wikiUserName: "RandomUser",
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...
 29     ackedAt: new Date(),
    ~~~~~~~~~~~~~~~~~~~~~~~~
 30   });
    ~~~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant