2
var config = require('config.json');
var mongo = require('mongoskin');
var db = mongo.db(config.connectionString, { native_parser: true });

module.exports.getNextSequence =  function (name) {
    var temp;
    db.collection("counters").findAndModify(
        { _id: name },           // query
        [],                      // represents a sort order if multiple matches
        { $inc: { seq: 1 } },    // update statement
        { new: true },           // options - new to return the modified document
        function (err, doc) {
            temp = doc.value.seq;
            console.log(temp);   // <-- here the temp is getting printed correctly
        }
    );
    return temp; 
}

Using the above code, I am not able to return the value of doc.value.seq. When doing console.log(obj.getNextSequence) it prints undefined.

I want the function to return the value of doc.value.seq.

0

2 Answers 2

1

I'm not familiar with mongoskin so I'm not positive this is correct, but a database query is typically asynchronous, so you need to access the queried value via a callback.

I'm guessing your "getNextSequence" function is returning the "temp" variable before the database query completes (i.e. before the "temp = doc.value.seq" statement).

Try something like this:

module.exports.getNextSequence =  function (name, callback) {
    var temp;
    db.collection("counters").findAndModify(
        { _id: name },           // query
        [],                      // represents a sort order if multiple matches
        { $inc: { seq: 1 } },    // update statement
        { new: true },           // options - new to return the modified document
        function (err, doc) {
            temp = doc.value.seq;
            callback(temp);
        }
    );
}

Then access "temp" from within the callback passed to getNextSequence.

Sign up to request clarification or add additional context in comments.

Comments

1

findAndModify is an asynchronous function. Your console.log line will run after you return temp, which will therefore be undefined. In order to get this to work, you'll want to use an asynchronous approach of your own. There are two available approaches in your situation.

Callbacks:

You're already using a callback, which you provide as the final argument to findAndModify. You could extend this approach and feed this into a callback of your own, as follows:

module.exports.getNextSequence =  function (name, callback) {
    db.collection("counters").findAndModify(
        { _id: name },
        [],
        { $inc: { seq: 1 } },
        { new: true },
        function (err, doc) {
            if (err) {
                return callback(err);
            }

            callback(null, doc.value.seq);
        }
    );
}

Of course, this will require you to pass a callback into getNextSequence and follow the callback pattern upstream. You might also want to handle the error from mongoskin and do some handling of your own.

Promises:

If you don't provide a callback to findAndModify, it will return a promise, which you can chain on to, as follows:

module.exports.getNextSequence =  function (name) {
    return db.collection("counters").findAndModify(
        { _id: name },
        [],
        { $inc: { seq: 1 } },
        { new: true }
    ).then(function (doc) {
        return doc.value.seq;
    });
}

Again, this will require you to follow the promise pattern upstream. You'll want to read up on promises if you choose this approach, so that you can correctly handle errors, which I have not addressed in the example above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.