Javascript 如何从客户端的Meteor.call方法返回结果?
我正在通过Javascript 如何从客户端的Meteor.call方法返回结果?,javascript,asynchronous,meteor,callback,Javascript,Asynchronous,Meteor,Callback,我正在通过Meteor.call()函数从客户端代码调用服务器方法 但是,当我试图将回调函数的返回值传递给onCreated函数中的var时,我得到了一个未定义的值 查看此解决方案,结果仍然仅在回调范围内可用: 另外,当我阅读Meteor调用的文档时,它解释了该方法是异步的,这说明函数的返回值不能分配给变量: 问题: import { Template } from 'meteor/templating'; import { Meteor } from 'meteor/meteor'; im
Meteor.call()
函数从客户端代码调用服务器方法
但是,当我试图将回调函数的返回值传递给onCreated
函数中的var时,我得到了一个未定义的值
查看此解决方案,结果仍然仅在回调范围内可用:
另外,当我阅读Meteor调用的文档时,它解释了该方法是异步的,这说明函数的返回值不能分配给变量:
问题:
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方法返回结果
客户端代码的代码片段:
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
函数不返回任何内容,因此mecObjects
的值将始终是未定义的
。服务器的方法调用响应(或错误)将只传递给回调,就像在代码中一样,实际上:那些error
和result
变量。您的mecObjects
的值将始终是未定义的,因为Meteor.call
函数不返回任何内容。服务器的方法调用响应(或错误)将只传递给回调,就像在您的代码中一样,实际上是:那些error
和result
变量。因为return语句在回调中并且是unblock,所以您不能返回这样的方法值。您可以将它放在将返回值的位置,但它必须位于帮助程序或反应式环境中。因为return语句位于回调中并且是unblock,所以您不能返回这样的方法值。您可以将其放置在返回值的位置,但它必须位于助手或反应式环境中。简短回答:
在公共代码(服务器和客户端可用的代码)中定义Meteor.Method,然后使用Meteor.apply
选项{returnStubValue:true}
mecObjects = Meteor.apply(method, [Constants.AppConfigKeys.meccalendarInitialValues, this.historyDays], { returnStubValue: true }, (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;
}
);
console.log(mecObjects) //prints the client-side result of the meteor method
详细回答:
Meteor允许在浏览器上模拟服务器方法的乐观UI。要启用此功能,请在公共代码(服务器+客户端空间)中定义Meteor.Method。这很有用,因为用户可以更快地看到UI更新,因为我们不需要进行服务器往返
注意,在上图中,方法调用首先在客户机上运行,然后在服务器上运行{returnStubValue:true}
允许调用方从客户端运行中接收方法的返回值
在编写Meteor方法时,可以使用this.isSimulation
指定哪些逻辑只在客户端或服务器上运行。此检查之外的任何代码都将同时在和中运行
请注意,在上图中,服务器仅运行console.log(“hi i m server”)
,而只有浏览器运行console.log(“hi i m client”)
。两者都在模拟
检查之外运行代码
一些关注:
如果服务器返回值与客户端返回值不同怎么办
结果!=因此,您需要正确处理这种情况
如果客户端运行失败怎么办?服务器是否仍在运行
是的,服务器仍在运行。但是,您可以阻止服务器在客户端上运行
添加另一个选项时失败,throwstubeexceptions:true
。此选项将
抛出可以在try-catch中捕获的客户端异常
如果客户端和服务器mongo更新不同怎么办
服务器mongo更改覆盖客户端mongo更改
更多信息请访问
和简短回答:
在公共代码(服务器和客户端可用的代码)中定义Meteor.Method,然后使用Meteor.apply
选项{returnStubValue:true}
mecObjects = Meteor.apply(method, [Constants.AppConfigKeys.meccalendarInitialValues, this.historyDays], { returnStubValue: true }, (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;
}
);
console.log(mecObjects) //prints the client-side result of the meteor method
详细回答:
Meteor允许在浏览器上模拟服务器方法的乐观UI。要启用此功能,请在公共代码(服务器+客户端空间)中定义Meteor.Method。这很有用,因为用户可以更快地看到UI更新,因为我们不需要进行服务器往返
注意,在上图中,方法调用首先在客户机上运行,然后在服务器上运行{returnStubValue:true}
允许调用方从客户端运行中接收方法的返回值
在编写Meteor方法时,可以使用this.isSimulation
指定哪些逻辑只在客户端或服务器上运行。此检查之外的任何代码都将同时在和中运行
请注意,在上图中,服务器仅运行console.log(“hi i m server”)
,而只有浏览器运行console.log(“hi i m client”)
。两者都在模拟
检查之外运行代码
一些关注:
如果服务器返回值与客户端返回值不同怎么办
结果!=因此,您需要正确处理这种情况
如果客户端运行失败怎么办?服务器是否仍在运行
是的,服务器仍在运行。但是,您可以阻止服务器在客户端上运行
添加另一个选项时失败,throwstubeexceptions:true
。此选项将
抛出可以在try-catch中捕获的客户端异常
如果客户端和服务器mongo更新不同怎么办
服务器mongo更改覆盖客户端mongo更改
更多信息请访问
而且您应该尝试创建一个模板反应变量。在Meteor.call的函数中指定该变量作为template.instance().x.set(result)
您实际想做什么?从onCreate()返回值对您来说意味着什么?即使没有方法调用,这对我来说也没有任何意义