I am upserting MongodB collections and within the function I also tested to see if it has been written to correctly. But externally the result is undefined? I have added Futures and Promises already so if anyone has experienced these kind of issues it will be much appreciated.
import Promise from 'bluebird';
var Future = Npm.require('fibers/future');
const callAsync = Promise.promisify(Meteor.call);
writeOrUpdateSparkdB: function (jsonObj) {
var future = new Future();
try {
if (jsonObj.content == "200 No results") {
console.log('inside writeOrUpdateSpark when 200 No result is : ', jsonObj.content);
future.return(jsonObj);
}
var respStatusCodeStrippedString = jsonObj.content.replace("200", ""); //string replace
var contentObj = EJSON.parse(respStatusCodeStrippedString);
var writeToSparkdB = contentObj.records.forEach((record) => {
FutureTasks.upsert({
number: record.number,
assignment_group: record.assignment_group
}, {
// Modifier
$set: {
userid: record.assigned_to_userid,
username: record.assigned_to,
start_date: moment(record.start_date, "YYYY-MM-DD HH:mm:ss").subtract(1, 'hours').format("YYYY-MM-DD HH:mm:ss"),
end_date: moment(record.end_date, "YYYY-MM-DD HH:mm:ss").add(1, 'hours').format("YYYY-MM-DD HH:mm:ss"),
} //End of object
}); //End of $set
}); //End of forEach
// Test Readback from Collections
var readBackFromSparkdB = FutureTasks.find().fetch();
console.log('Read back from FutureTasks.find().fetch() inside writeOrUpdateSparkdB : ', FutureTasks.find().fetch()); <- OK to read back here.
console.log('inside writeOrUpdateSparkdB test_time FutureTask Collection State : ', writeToSparkdB); OK to read back here
console.log('inside writeOrUpdateSparkdB test_time FutureTask Collection State : ', writeToSparkdB); //undefined
console.log('typeof FutureTask.find is : ', typeof(FutureTasks.find())); //object
console.log('typeof FutureTask.find().fetch() is : ', typeof(FutureTasks.find().fetch())); //object
future.return(FutureTasks.find().fetch());
} //End of try
catch (err) {
future.throw("upsert-error", err);
}
finally {
future.return(FutureTasks.find().fetch());
}
return future.wait();
}, // End of writeOrUpdateSparkdB
Calling doesn’t return collections even though it is available when read back from within
function writeOrUpdatedB?
async function doSomething(userToBeChanged) {
const res1 = await callAsync('writeOrUpdateSparkdB', res0);
const tp2 = await console.log('outside writeOrUpdateSparkdB is : ', res1); //Can't read here?
const typeof_tp2 = await console.log('typeof outside writeOrUpdateSparkdB is : ', typeof(res1)); //Can't find?
}
where res0 is coming from previous HTTP.get jsonObj
when I say not returning i mean from res1, but it is still updating the collections globally inside the function?