I am executing a Meteor method within a forEach
function, and pushing the result of each method (a new id
) into an array that is then used for other purposes. I can get see each new id
created (the result
from each method). However, the console is returning what appears to be an array of index numbers, rather than an array of new id
s.
CODE EDITED TO ADDRESS ANSWER BELOW In event handler, with two documents selected:
var selectionsFinal = [];
selections.forEach(function(v, selectionsFinal) {
Meteor.call('copy', v, function(error, result) {
if(error) {
console.log(error);
} else {
console.log("Result: ", result); //properly returns new id
selectionsFinal.push(result);
console.log("SelectionsFinal: ", selectionsFinal); // properly returns array of 2 new ids
}
});
console.log("SelectionsFinal: ", selectionsFinal); //should return array of new ids, returns []
});
console.log("Selections Final: ", selectionsFinal); //should return array of new ids, returns []
Method
copy: function(v) {
check(v, String);
var valuation = Valuations.findOne({_id:v},{fields:{_id:0});
return Valuations.insert(valuation)
}