Sunday 28 May 2017

Iterating over data from mongoose in node

I have this fiddle that illustrates what I want to do and it works as intended: https://jsfiddle.net/mbLwsnak/

All it does is loop through an array of objects, and adds a key value pair.

I have this same code in my node app, and it doesn't work. The new pair is not added to the objects, even though the code is practically the same, so I figure it must be the context.

Schema.find()
.then(function(currencydata){

    var data = currencydata;

    data.forEach(function(obj){
        obj.thing = "Thing";
    })

    console.log(data);
    mdata.currencydata = data;

})
.then(function(result){
    res.render('index', mdata);
})

So the only difference I can think of that matters is that this is all inside an asynchronous function. I even tried putting the log inside a setTimeout in case it was something to do with being async, but no change.



via user3806429

No comments:

Post a Comment