When I make an API call, I want to update 2 separate collections with MongoDB. The official way of thing this will be using transactions but because this is a prototype for now, I will just use 2 separate function calls to update each collection.
Here is what I am doing:
async.waterfall([
function(callback) {
callback(null);
}, function(callback) {
connectToDatabase(MONGODB_URI)
.then(db => updateDocument1(arguments))
.then(result => {
callback(null);
});
}
, function(callback) {
connectToDatabase(MONGODB_URI)
.then(db => updateDocument2(arguments))
.then(result => {
callback(null);
});
, function(err, result) {
callback(null, null)
}
])
But somehow, it does not execute updateDocument2. Also, updateDocument1 stops in the middle of the process, so none of documents are updated. Do you know why? And Does anyone know a better way?