sh977218 sh977218 - 2 months ago 11x
Node.js Question

mongoose .save() doesn't work

I have those code running try to save an object into MongoDB.


never got success run. the code running fine.

method doesn't work.

var conn = mongoose.createConnection(mongoUrl, {auth: {authdb: "admin"}});
conn.on('error', function (err) {
throw err;
conn.once('open', function callback() {
console.log("connected to " + mongoUrl);
var cacheSchema = mongoose.Schema({}, {strict: false});
cacheSchema.set('collection', 'caches');
// you need to specify which connection is uing.
var Cache = conn.model('cache', cacheSchema);
var measure = new Cache();
measure['test'] = "test";{


Please read this part of documentation from mongoose and try the following:

   var measure = new Cache({test:'teste'}); 
   // or measure.set('test', 'teste'); (err) {

You will be able to see the issue if there's any.

Update the issue is using:

var Cache = conn.model('cache', cacheSchema);

instead of

var Cache = mongoose.model('cache', cacheSchema);