0

I have saved users

{ "_id" : ObjectId("5f970abfe1c19f452cf715eb"), "username" : "Jean1", "email" : "mtigana@aol.com", "__v" : 0 }
{ "_id" : ObjectId("5f97c071bad24d1a81b25dd1"), "username" : "Jim", "email" : "jimmana@aol.com", "__v" : 0 }

When I create Post

async function createPost() {
      await Post.create({title: 'Vou come Karen! ',
        body: 'What a wonderful life!',
        postedBy: 5f97c071bad24d1a81b25dd1,
        comments
      });

This gives error

        postedBy: 5f97c071bad24d1a81b25dd1,
                  ^

SyntaxError: Invalid or unexpected token

How to releate postedBy field with user Jim?

Richard Rublev
  • 7,718
  • 16
  • 77
  • 121

1 Answers1

1

You can use mongoose.Types.ObjectId()

Ex:-

 await Post.create({title: 'Vou come Karen! ',
        body: 'What a wonderful life!',
        postedBy:  mongoose.Types.ObjectId('5f97c071bad24d1a81b25dd1'),
        comments
      });
Nikhil G
  • 2,096
  • 14
  • 18