StackThis
StackThis

Reputation: 883

Bluebird Promisfy.each reference error?

Am new to promisification and am not quite sure if .then and .each carry variables across the entire promise.

Also, I clearly define docReplies in the fourth line, yet the console logs:

Possibly unhandled ReferenceError: docReplies is not defined

Am looking to loop over each element (replyID) in the repliesIDsArray and findOneAsync the message..then for each element in the doc.replies array find the index of the replyID, setting that to index1..then for each element in the doc.replies[index1] array find the index of the username (res.locals.username), setting that to index2..then with index1 and index2, save fields to save to the doc..

(Here's a link to where this derives, with an outline of the db schema if that helps)

Promise.each(repliesIDsArray, function(replyID){
  Models.Message.findOneAsync({'_id': req.params.message_id})
    .then(function(doc){
        var docReplies = [];
        pushReplies = docReplies.push(doc.replies);
    }).each(docReplies, function (replyIndex){
        // loop over doc.replies to find..
        // ..the index(index1) of replyID at replies[index]._id
        var index1;
        if (docReplies[replyIndex]._id == replyID) {
            index1 = replyIndex;
        }
        var docRepliesIndex1 = [];
        pushRepliesIndex1 = docRepliesIndex1.push(doc.replies[index1]);
    }).each(docRepliesIndex1, function (usernameIndex){
        // loop over doc.replies[index1].to and find..
        // ..the index(index2) of res.locals.username at replies[index1].to[index2]
        var index2;
        if (docRepliesIndex1.to[usernameIndex].username === res.locals.username) {
            index2 = usernameIndex;
        }
    }).then(function(index1, index2) {
        console.log('index1 = ' + index1);
        console.log('index2 = ' + index2);
        doc.replies[index1].to[index2].read.marked = true;
        doc.replies[index1].to[index2].read.datetime = req.body.datetimeRead;
        doc.replies[index1].to[index2].updated= req.body.datetimeRead;
        doc.markModified('replies');
        var saveFunc = Promise.promisify(doc.save, doc);
        return saveFunc();
        console.log('doc saved');
    }).then(function(saved) {
        console.log("Success! doc saved!");
        console.log("Sending saved doc:");
        res.json(saved);
    }).catch(Promise.OperationalError, function(e){
        // handle error in Mongoose findOne + save
        console.error("unable to save because: ", e.message);
        console.log(e);
        res.send(e);
        throw err;
    }).catch(function(err){
        // would be a 500 error, an OperationalError is probably a 4XX
        console.log(err);
        res.send(err);
        throw err; // this optionally marks the chain as yet to be handled
    });
})

Upvotes: 0

Views: 411

Answers (1)

jfriend00
jfriend00

Reputation: 707436

Promises have no magic capability with your variable declarations. docReplies is defined in your first .then() callback function and is only available within that function. If you want it available across many .then() handler functions, then you will need declare it at a higher scope so it's available everywhere (normal Javascript scoping rules).

Or, in certain cases, you can return data from one promise handler to another, but it doesn't sound like that's what you're trying to do here.

In any case, normal Javascript scoping rules apply to all variable declarations, even those in promise callback functions.

Upvotes: 1

Related Questions