I'm calling a server method from my client side code via the Meteor.call()
function.
But I get an undefined value when I try to pass the callback function's return value to a var within the onCreated
function.
Looking at this solution the result is still only available within the scope of the callback:
https://stackoverflow.com/a/31661065/1829251
Also when I read the docs for Meteor-call it explains that the method is async which explains that the return value of the function can't be assigned to a variable:
https://docs.meteor.com/api/methods.html#Meteor-call
Question:
How can you return a result from Meteor.call method in client?
Code snippet of client side code:
import { Template } from 'meteor/templating';
import { Meteor } from 'meteor/meteor';
import { Datasets } from '../../../../api/datasets/datasets.js';
import { Constants } from '../../../../startup/constants.js';
import './rollup-history.html';
import './rollup-history.scss';
Template.rollup_history.onCreated(function() {
this.historyDays = 7;
this.rollupHistoryMECTitles = () => {
let mecObjects = [];
// Query the MEC calendar dates from AppConfiguration collection
let method = Constants.Methods.getAppConfigCollectionFromKeyDateSorted;
mecObjects = Meteor.call(method, Constants.AppConfigKeys.meccalendarInitialValues, this.historyDays, (error, result) => {
if(error){
console.log("Error retrieving MEC Dates" + error);
}
//Logging values here shows the values as expected
console.log("MEC Dates in View" + JSON.stringify(result));
return result;
}
);
//logging value here shows undefined value
console.log("mec objects: " + JSON.stringify(mecObjects));
return mecObjects;
};
});
Template.rollup_history.helpers({
mecHeaderColumns: function() {
return Template.instance().rollupHistoryMECTitles();
},
});
Meteor.call
asTemplate.instance().x.set(result)
- Ankur Soni