The following snippet of code will save the document to the database, however it will not throw an error or the socket1 emit. The socket2 emit will work however. What is the error in this query?
db.doc.findOne({field1: value1}, function(err, doc) {
if (!doc) {
db.doc.save({field1: value1, field2: value2, field3: value3}, function(err, saved) {
if( err || !saved ) console.log("User not saved");
else socket.emit('socket1', value1, value2);
});
}
else {
socket.emit('socket2', value1);
}
});
You may try save({...}, {safe:true}, function(...) {...})