How to iterate collections in forEach and inside I would like to perform upsert


I have a collection which I iterate to read from

FutureTasks.find().forEach(function (details) {

asyncPromiseHttpFunction( )
.then( txt => console.log('returned value is :' ,txt) // whatever value i.e. 204, 500 etc   

the asyncPromiseHttpFunction reads in each row and makes a http request and dynamically gets back status
code, so how to store this status code on the fly?

at the moment it is just console.log which outputs the correct status code but I was thinking something along the line of

.then(txt => FutureTasks.upsert({}, {
       $set: {
            status: txt,
                } // End of object
              }) // End of $set

But the status code that was previously available from console.log is not updating the status code collections attribute?

My collection is something like

 Schemas.FutureTask = new SimpleSchema({
    number: {
        type: String,
        label: "TICKET_NUMBER",
        max: 10,  
        index: true,
        unique: true
    userid: {
        type: String,
        label: "USER_ID",
        max: 10 
    status: {   <---- this is going to be updated to be either 200, 500 etc
        type: Number,
        label: "JOB_STATUS",
        max: 25,
        optional: true

var Collections = {};
FutureTasks = Collections.FutureTasks = new Mongo.Collection('future_tasks');


Are you using Meteor’s inbuilt Promises, or are you using Bluebird?

If you check my answer here:

You will see that Bluebird Promises do not work properly with Meteor’s sync-style MongoDB functions.


Hi Rob,

Neither, I am using RVSP.Promise which is a popular Promise library


Well, OK - I would think that will have the same issues as Bluebird.

Meteor’s inbuilt Promises also wrap the success and fail callbacks in fibers so they work seamlessly with sync-style Mongo.

If you really must use RSVP, then you can re-wrap the standard asynchronous Mongo functions in Promises, in which case they will work as you expect (assuming RSVP works like Bluebird).

Is there any reason you’re not using Meteor’s standard Promises?


Hi Rob

if the .then(txt => console.log(txt)) inside the forEach loop produces the desired output with RVSP.Promise and I just instead need to store in collections instead of console.log, how come I need to use the default Promise?



Changed to default ecmascript Promise, but still no update appearing in collection status field?


You are setting number and userId? These are not optional according to your schema.


status is going to be updated depending on status code from async http request

.then(txt => FutureTasks.upsert({

                                userid: details.userid,
                                number: details.number,
                            }, {
                                $set: {
                                    status: txt,
                        .catch(reason => details.status = reason);


That still doesn’t match your schema.


Basically the userid and number coming in from one service that doesnt change, but the status will be the eventually updated with status code 200 etc


On meteor mongo db shell, below works

meteor:PRIMARY> db.tasks.update({_id:‘10002’},{$set:{loginUser: “SudhaVazarala”,events:[{eOrganiser:“SureshA”,eSubject:“Going to Mumbai on Oct 1st @ 7:30 am IST”,eLocation:" from pune in car",eDate:ISODate(“2016-10-01T00:00:00Z”),eStartTime:“07:30AM”,eEndTime:“09:30AM”,eMobileMode:“regular”,ePriority:“0”}]}},{upsert:true});
WriteResult({ “nMatched” : 1, “nUpserted” : 0, “nModified” : 1 })

In my nodejs /meteor program it fails
Tasks.update({_id:‘10003’},{$set:{loginUser: “SureshAdapa”,
eSubject:“Discussion on literally canvas on Oct 10th @ 7:30 am IST”,
eLocation:" Mobile call",


W20160929-17:54:02.714(5.5)? (STDERR) Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.
W20160929-17:54:02.720(5.5)? (STDERR) at Object.Meteor._nodeCodeMustBeInFiber (packages/meteor/dynamics_nodejs.js:9:1)
W20160929-17:54:02.723(5.5)? (STDERR) at Object.Meteor.bindEnvironment (packages/meteor/dynamics_nodejs.js:85:1)
W20160929-17:54:02.726(5.5)? (STDERR) at MongoConnection. (packages/meteor/helpers.js:117:1)
W20160929-17:54:02.728(5.5)? (STDERR) at MongoConnection.(anonymous function) [as update] (packages/mongo/mongo_driver.js:771:49)
W20160929-17:54:02.732(5.5)? (STDERR) at [object Object].update (packages/mongo/collection.js:589:29)
W20160929-17:54:02.735(5.5)? (STDERR) at [object Object].Mongo.Collection.(anonymous function) [as update] (packages/aldeed_collection2-core/lib/collection2.js:203:1)
W20160929-17:54:02.738(5.5)? (STDERR) at server/main.js:165:12