Bim Bam
Bim Bam

Reputation: 439

How to use embedded Object in mongoose Schemas?

I have Schema:

var someSchema = new Schema({
    data: {
        nickname: {type: String},
        id: Schema.Types.ObjectId,
        message: { type: String},
    },
    createdAt: { type: Date, default: Date.now }
});

But when I am trying to use it:

some.collection.insert({data.nickname: nickname, data.message: message,}, function (err, doc) {
  if (err) {
      console.log("Something wrong !");
  }
  res.redirect('/');
});

I have an error: SyntaxError: Unexpected token .

How to insert data to my Object?

Upvotes: 0

Views: 696

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 311865

You need to use the same nested object syntax when defining the document to insert:

some.collection.insert({data: {nickname: nickname, message: message}}, function(err, doc) {
  if (err) {
      console.log("Something wrong !");
  }
  res.redirect('/');
});

Upvotes: 1

Related Questions