Using Mongoose promises to do find and update - node.js

I'm trying to using the Mongoose Promises to have a cleaner code (see nested functions).
Specifically, I'm trying to build something like this:
Model.findOne({_id: req.params.id, client: req.credentials.clientId}).exec()
.then(function(resource){
if (!resource) {
throw new restify.ResourceNotFoundError();
}
return resource;
})
.then(function(resource) {
resource.name = req.body.name;
return resource.save; <-- not correct!
})
.then(null, function(err) {
//handle errors here
});
So, in one of the promises I would need to save my model. As of the latest stable release, Model.save() does not return a promise (bug is here).
To use the classical save method, I could use this:
//..before as above
.then(function(resource) {
resource.name = req.body.name;
resource.save(function(err) {
if (err)
throw new Error();
//how do I return OK to the parent promise?
});
})
But as also commented in the code, how do I return to the holding promise the return value of the save callback (which runs async)?
Is there a better way?
(btw, findOneAndUpdate is a no-go solution for my case)

One way of doing it would be to wrap the .save code in your own method which returns a promise. You'll need a promise library, like RSVP or Q. I'll write it in RSVP, but you should get the idea.
var save = function(resource) {
return new RSVP.Promise(function(resolve, reject) {
resource.save(function(err, resource) {
if (err) return reject(err);
resolve(resource);
});
});
}
Then in your calling code:
// ...
.then(function(resource) {
resource.name = req.body.name;
return save(resource);
})
.then(function(resource) {
// Whatever
})
.catch(function(err) {
// handle errors here
});
The other way of doing it would be to nodeify the save method, but I'd do it they way I've detailed above.

Related

await doesn't wait for function to end

I know this has been asked a lot but I can't seem to use the existing answers to get my code to work. I am trying to use mongojs to make a single query, then put the results in a global (relative to the scope) variable to avoid nesting multiple callbacks. However, I can't seem to get the code to wait for the query to end before continuing.
async function taskmaster() {
const db = mongojs(mongoUri.tasker);
let currentDoc;
const getTask = async function() {
db.tasks.findOne({'task_id': {'$in': [taskId, null]}}, function(err, doc) {
console.log(doc);
currentDoc = doc;
});
}
await getTask();
console.log(currentDoc);
// Code to process currentDoc below
}
No matter what I do, console.log(doc) shows a valid MongoDB document, yet console.log(currentDoc) shows "undefined". Thanks in advance!
Inside your async function, you use findOne method() in callback style, so it's totally normal that console.log(currentDoc) shows undefined, because it executes before
currentDoc = doc;
You can promisify the findOne method, to use it with async/await keyword.
I found a tutorial to promisfy a callback style function here, hope it help : https://flaviocopes.com/node-promisify/
--- EDIT ---
I rewrite your code when promising the findOne method, as suggested by O.Jones
async function taskmaster() {
const getTask = async (taskId) => {
return new Promise((resolve, reject) => {
db.tasks.findOne({'task_id': {'$in': [taskId, null]}}, function(err, doc) {
if(err) {
console.log("problem when retrieve data");
reject(err);
} else {
resolve(doc);
}
});
})
const db = mongojs(mongoUri.tasker);
const currentDoc = await getTask(taskId);
console.log(currentDoc);
}

Mongoose: write own methods with promise

I want to write some instance / static methods for a model, which uses the mongoose's API and do something before and after using the mongoose's API.
For example, I write my own Article.createArticle method, it checks the data before Article.create, and return article.toObject() after creation.
this is how I want my createArticle works:
Article.createArticle({someKeys: 'SomeData', ...})
.then(obj => {
// get plain object here
});
I tried to write something like this:
Article.Schema.static({
createArticle: function(data) {
return new Promise(function(resolve, reject){
checkDataKeys(data);
resolve(mongoose.model('Article').create(data)
.then(article => resolve(article.toObject()));
);
});
},
});
with this createArticle, I only get undefined in then,
I must get something wrong.
Also, in addition to make the createArticle work, is there any way to make the code more elegant?
Thanks.
I myself found a methods that works for me. Though I'm not very understand the mechanism, maybe I'll try to work on it later...
ArticleSchema.static({
createArticle: function(data) {
checkDataKeys(data); // pre works
return mongoose.model('Article').create(data)
.then(obj => {
return obj.toObject(); // afterworks
// here, just get the plain obj from doc
// and *return* it
});
},
});
update: after I searched something about Promise, maybe this could be better.
ArticleSchema.static({
createArticle: function(data) {
checkDataKeys(data); // pre works
return mongoose.model('Article').create(data)
.then(obj => {
return Promise.resolve(obj.toObject());
// Some say that this is totally same as directly return
})
.catch(err => {
return Promise.reject(err);
// if error, this will ensure the err will be caught
// for some async cases
});
},
});

NodeJS fs.appendFile not working within promise in mocha

I want to keep a log during my integration test suite. I'm testing that every 'item' is being compiled and logging how much time it took. I'm using node 4.3.
First of all I create the log file:
before(function() {
if (!fs.existsSync("./log.csv")) {
fs.writeFile("./log.csv", "Name; Time");
}
});
Then within each it block I would do this:
for (const item of items) {
it('compiles', function() {
return item.testCompile();
});
}
And item class has these methods:
testCompile() {
return this
.buildItem()
.then(result => {
// whatever testing stuff
});
}
buildItem() {
return this
.internalLogicForCompiling()
.then(result => {
// This is not appending anything
fs.appendFile("./log.csv", `${item.name}; ${result.compileTime}`);
return result;
});
}
So far the file is created but never updated... Any clue what I'm doing wrong?
PS: I assume if the file doesn't exists, fs should throw an error, however it doesn't.
Your code is generally ignoring the fact that your fs calls are asynchronous. Promises are not magic. If you use code that is asynchronous but does not use promises, you need to do more than plop that code in a promise can call it done.
The easiest way to deal with the issue would be to use fs.writeFileSync and fs.appendFileSync instead of the calls you make. Otherwise, you should write your before like this:
before(function(done) {
if (!fs.existsSync("./log.csv")) {
fs.writeFile("./log.csv", "Name; Time", done);
}
});
I've just added the done callback.
And buildItem could be something like this so that the promise it returns won't resolve before appendFile is done doing its work:
buildItem() {
return this
.internalLogicForCompiling()
.then(result => {
return new Promise((resolve, reject) => {
fs.appendFile("./log.csv", `${item.name}; ${result.compileTime}`, (err) => {
if (err) {
reject(err);
return;
}
resolve(result);
});
});
});
}

How to handle conditional callbacks using Promises(Bluebird) in NodeJS

I am currently trying to refactor the codebase that I have and want to have a more developer friendly codebase. Part 1 of that is changing callback to Promises. Currently, at some places we are using Async.waterfall to flatten the callback hell, which works for me. The remaining places where we couldn't was because they were conditional callbacks, That means different callback function inside if and else
if(x){
call_this_callback()
}else{
call_other_callback()
}
Now I use bluebird for promises in Node.JS and I can't figure out how to handle conditional callback to flatten the callback hell.
EDIT
A more realistic scenario, considering I didn't get the crux of the issue.
var promise = Collection1.find({
condn: true
}).exec()
promise.then(function(val) {
if(val){
return gotoStep2();
}else{
return createItem();
}
})
.then(function (res){
//I don't know which response I am getting Is it the promise of gotoStep2
//or from the createItem because in both the different database is going
//to be called. How do I handle this
})
There is no magic, you can easily chain promises with return inside a promise.
var promise = Collection1.find({
condn: true
}).exec();
//first approach
promise.then(function(val) {
if(val){
return gotoStep2()
.then(function(result) {
//handle result from gotoStep2() here
});
}else{
return createItem()
.then(function(result) {
//handle result from createItem() here
});
}
});
//second approach
promise.then(function(val) {
return new Promise(function() {
if(val){
return gotoStep2()
} else {
return createItem();
}
}).then(function(result) {
if (val) {
//this is result from gotoStep2();
} else {
//this is result from createItem();
}
});
});
//third approach
promise.then(function(val) {
if(val){
return gotoStep2(); //assume return array
} else {
return createItem(); //assume return object
}
}).then(function(result) {
//validate the result if it has own status or type
if (Array.isArray(result)) {
//returned from gotoStep2()
} else {
//returned from createItem()
}
//you can have other validation or status checking based on your results
});
Edit: Update sample code because author updated his sample code.
Edit: Added 3rd approach to help you understand promise chain
Here is the answer to the promise branch: nested and unnested.
Make the branches and don't join them together.

Way to use mongodb's find cursor's next/each functions as a promise after promisifying mongodb with Bluebird

The node mongodb docs specify to use next/each for large number of documents so as to not have everything loaded onto memory if we were to use toArray.
So, i thought my sample code should work as is. But it just returns one document.
What should be the correct way to deal with this problem?
This is my code sample :
var findAsync = function (collection,query) {
return mongodb.MongoClient.connectAsync(mongodbServerString)
.then(function (db) {
return [db.collection(collection).find(query), db];
});
};
findAsync("UserProfile",{})
.spread(function (cursor,db) {
return [cursor.project({Email:true}),db];
})
.spread(function (cursor, db) {
return cursor.eachAsync().then(function (doc) {
console.log(doc);
}).catch(function () {
db.close();
});
});
Promises represent singular values. Promises are basically like function returns, since a function cannot return multiple values - it wouldn't make sense to convert each to a promise returning function.
What you can do is either convert it to an Observable returning function and then use .forEach on that to get a promise back for the completion of the sequence or you can implement something similar manually:
function each(cursor, fn) {
return new Promise((resolve, reject) => {
cursor.forEach((err, data) => {
if(err) {
cursor.close();
return reject(err);
}
try { fn(data); } catch(e) { cursor.close(); reject(e); }
}, err => { { // finished callback
if(err) reject(err);
else resolve();
});
});
}
Which would let you write:
each(cursor, doc => console.log(doc)).then(...).catch(...)
Also note that Mongo connections are persistent, you're supposed to connect once when the server starts and then keep the connection open for as long as the server is run.

Resources