torbenrudgaard
torbenrudgaard

Reputation: 2561

Very simple Mongo .save gives no error and saves no record

I tried several examples from here, but none seems to work. I must be missing a small important thing.

This is my model

var mongoose = require('mongoose');
var Schema   = mongoose.Schema;
var mySchema = new Schema({
    _id:  Schema.Types.ObjectId, 
    foo:  String,
    bar:  String
}, { versionKey: false }); 
module.exports = mongoose.model('myModel', mySchema, 'my');

And this is my code

let myModel= require('/models/myModel');

let myRec = new myModel({
    "foo" : "whatever",
    "bar" : "whatever"
});

myRec.save(function (err, res) {  
    if (err) {
        console.log("ERROR " + err);
    } else {
        console.log("Saved " + res);
    }
})

I dont get ERROR or Saved, and it does not save anything in the database either.

Can anyone see what I am missing here?

Upvotes: 0

Views: 71

Answers (1)

vishnu
vishnu

Reputation: 2011

Since you have explicitly declared the _id field in your Schema while creating a document you need to pass the _id field also. You can remove _id if you want so Mongoose will automatically create _id of type ObjectId for the document

Upvotes: 1

Related Questions