Node & Mongoose - Error while saving: TypeError: Object.keys caused by a non-object

In the user schema below, there is a foobar.events field that I am trying to push new hashes (obtained from a POST API request).

 var userSchema = mongoose.Schema({ foobar: { id : String, token : String, email : String, name : String, events : [{ action : String, timestamp : Date, user_xid : String, type : {type: String}, event_xid : String }] } }); 

And here is the logic of this express route:

 app.post('/foobar/post', function(req, res) { var jb_user_xid = req.body['events'][0]['user_xid']; var jb_timestamp = req.body['events'][0]['timestamp']; var jb_action = req.body['events'][0]['action']; var jb_type = req.body['events'][0]['type']; var jb_event_xid = req.body['events'][0]['event_xid']; User.findOne({'foobar.id':jb_user_xid}, function(err, user) { console.log(user); user.foobar.events.push({ user_xid: jb_user_xid, timestamp: jb_timestamp, action: jb_action, type: jb_type, event_xid: jb_event_xid }); user.save(function(err) { if (err){ console.log("Error on save: " + err); } else { console.log("Save successful"); } }); }); res.writeHead(200); res.end(); return; }); 

The find method succeeds, but when you try to save to the database, the following error occurs: Error on save: TypeError: Object.keys called on non-object - any idea why this error occurs?

There was a similar issue in this thread , but changing findOne to findById violated my user request.

As a side note, this is what is returned in req.body from the API:

 { events: [ { action: 'updation', timestamp: 1408846680, user_xid: 'aguxwNqb_Xg87buMyP6Wiw', type: 'move', event_xid: 'vhAkgg1XwQvLynAkkCc8Iw' } ], notification_timestamp: 1408846680 } 

And here is what came back from the User.findOne method

 { __v: 17, _id: 53f7d23e432de20200970c10, foobar: { id: 'aguxwNqb_Xg87buMyP6Wiw', name: 'Test User', token: 'W3AjaI7_iOWilcKRpmxenQWi', events: [] } } 
+5
source share
2 answers

This error was actually caused by old data in my Mongo database. The events field was filled with extra lines. I deleted them and my original code started to work successfully. There were no changes to the above code.

+5
source

I tried your code and it works great .. for me .. try checking out the mongoose module version or something. If you still have a problem, please do so using the update function, not save. This will be more performance oriented.

this is the following:

  var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/test'); var db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); db.once('open', function callback () { // yay! }); var userSchema = mongoose.Schema({ foobar: { id : String, name : String, events : [{ action : String, timestamp : Date, user_xid : String, type : {type: String}, event_xid : String }] } }); var User = mongoose.model('user', userSchema); /* //used for saving. var person = new User({ foobar: { id: 'aguxwNqb_Xg87buMyP6Wiw', name: 'Test User', token: 'W3AjaI7_iOWilcKRpmxenQWi', events: [] } }); person.save(function(err,data){ console.log(err); console.log(data); }) */ User.findOne({'foobar.id':'aguxwNqb_Xg87buMyP6Wiw'}, function(err, user) { console.log(user); user.foobar.events.push({ action: 'updation', timestamp : 1408846680, user_xid: 'aguxwNqb_Xg87buMyP6Wiw', type: 'move', event_xid: 'vhAkgg1XwQvLynAkkCc8Iw' }); user.save(function(err) { if (err){ console.log("Error on save: " + err); } else { console.log("Save successful"); } }); }); 
+1
source

Source: https://habr.com/ru/post/1201268/


All Articles