Vivek P
Vivek P

Reputation: 3050

ERROR: Cannot read property '_id' of undefined

I am trying to create a record using mongoose.js framework method model.Create as below example, i am geting an error as Cannot read property '_id' of undefined

var schema = new mongoose.Schema({ name: 'string', size: 'string' });

    var Tank = mongoose.model('Tank', yourSchema);

    var Aryobj = [{ size: 'small' }, {size:'large'}];

    Tank.create(Aryobj, function (err, small, large) {
      if (err) return handleError(err);
       console.log("small ID:"+small._id);
       console.log("large ID:"+large._id);

    })

ther is any problem my code? any help is appreciated.

Upvotes: 1

Views: 2817

Answers (1)

eoinbrazil
eoinbrazil

Reputation: 425

I think it is just a typo in your code if you change the line (replace yourSchema with schema)

var Tank = mongoose.model('Tank', yourSchema);

to

var Tank = mongoose.model('Tank', schema);

As the definition is for the var schema

var schema = new mongoose.Schema({ name: 'string', size: 'string' });

I ran the script with the typo corrected and it outputted two IDs.

Upvotes: 3

Related Questions