Meteor 1.6 在客户端和服务器端进行同步调用
Meteor 1.6 make synchronous call both client and server side
我目前正在开发一个集合管理器,它也管理外键。
它还会生成一些表单,我很难检查外键并获得对客户端的正确回调。
我正在使用 meteor 的 wrapAsync 方法,以便使用方法调用的同步版本。
首先,我在 "check fk" 方法中声明对函数的同步调用。
declareCheckFKMethod(index){
Meteor.methods({
[this._collectionList[index]._prefix.replace(/\//g,"")+this._checkFKSuffix]:(fk)=>{
var syncFunc = Meteor.wrapAsync(this.checkFKInDB.bind(this));
return syncFunc(fk,index)
}
})
}
这是目标函数:
checkFKInDB(fk,collectionIndex,callBack){
try{
var test = this._collectionList[collectionIndex].find({_id:fk}).fetch();
if(test.length==1){
return callBack(null,true);
}
else{
return callBack(null,false);
}
}
catch(e){
return callBack(new Meteor.Error("DB error", e.message),null)
}
}
然后在我的插入函数中检查客户端和服务器端的所有 FK 字段:
const check = Meteor.call(this._collectionList[index]._prefix.replace(/\//g,"")+this._checkFKSuffix,document[element.entryName]);
console.log(check)
这就是我插入有效文档时得到的结果:
服务器端控制台日志:true
客户端控制台日志:未定义
我怀疑客户端根本就没有在等待回调,而服务端却在等待。
我该如何解决?
(顺便说一句,我尝试了 await/async 个关键字,但它只会给我错误...)
您的客户端函数 总是 需要 to pass a callback 才能接收异步结果:
Meteor.call(this._collectionList[index]._prefix.replace(/\//g,"")+this._checkFKSuffix,document[element.entryName], (err, check) => {
console.log(err, check);
});
编辑:请注意,Meteor 方法将解析方法 Promise 样式中的值(阅读有关 Fibers 工作原理的更多信息),而您的客户端将始终以回调样式接收结果或错误。因此,在调用方法时在客户端上使用某种异步/等待样式模式的想法可能无法实现。
我实际上找到了一个解决方案,但我不确定它为什么有效,如果有人能清楚地解释:
我只需要像这样制作 "checkFKInDB" 服务器和客户端功能 :
checkFKInDB(fk,collectionIndex,callBack){
if(Meteor.isClient){
try{
var test = this._collectionList[collectionIndex].find({_id:fk}).fetch();
if(test.length==1){
return true;
}
else{
return false;
}
}
catch(e){
throw new Meteor.Error("DB error", e.message);
}
}
if(Meteor.isServer){
try{
var test = this._collectionList[collectionIndex].find({_id:fk}).fetch();
if(test.length==1){
return callBack(null,true);
}
else{
return callBack(null,false);
}
}
catch(e){
throw new Meteor.Error("DB error", e.message)
}
}
}
然后:
try{
test = Meteor.call(this._collectionList[index]._prefix.replace(/\//g,"")+this._checkFKSuffix,document[element.entryName]);
}
catch(e){
throw new Meteor.Error("DB error",e.message)
}
如果有人能解释为什么服务器端需要回调,而客户端不需要回调,那就太好了!
我目前正在开发一个集合管理器,它也管理外键。 它还会生成一些表单,我很难检查外键并获得对客户端的正确回调。
我正在使用 meteor 的 wrapAsync 方法,以便使用方法调用的同步版本。
首先,我在 "check fk" 方法中声明对函数的同步调用。
declareCheckFKMethod(index){
Meteor.methods({
[this._collectionList[index]._prefix.replace(/\//g,"")+this._checkFKSuffix]:(fk)=>{
var syncFunc = Meteor.wrapAsync(this.checkFKInDB.bind(this));
return syncFunc(fk,index)
}
})
}
这是目标函数:
checkFKInDB(fk,collectionIndex,callBack){
try{
var test = this._collectionList[collectionIndex].find({_id:fk}).fetch();
if(test.length==1){
return callBack(null,true);
}
else{
return callBack(null,false);
}
}
catch(e){
return callBack(new Meteor.Error("DB error", e.message),null)
}
}
然后在我的插入函数中检查客户端和服务器端的所有 FK 字段:
const check = Meteor.call(this._collectionList[index]._prefix.replace(/\//g,"")+this._checkFKSuffix,document[element.entryName]);
console.log(check)
这就是我插入有效文档时得到的结果:
服务器端控制台日志:true
客户端控制台日志:未定义
我怀疑客户端根本就没有在等待回调,而服务端却在等待。 我该如何解决? (顺便说一句,我尝试了 await/async 个关键字,但它只会给我错误...)
您的客户端函数 总是 需要 to pass a callback 才能接收异步结果:
Meteor.call(this._collectionList[index]._prefix.replace(/\//g,"")+this._checkFKSuffix,document[element.entryName], (err, check) => {
console.log(err, check);
});
编辑:请注意,Meteor 方法将解析方法 Promise 样式中的值(阅读有关 Fibers 工作原理的更多信息),而您的客户端将始终以回调样式接收结果或错误。因此,在调用方法时在客户端上使用某种异步/等待样式模式的想法可能无法实现。
我实际上找到了一个解决方案,但我不确定它为什么有效,如果有人能清楚地解释:
我只需要像这样制作 "checkFKInDB" 服务器和客户端功能 :
checkFKInDB(fk,collectionIndex,callBack){
if(Meteor.isClient){
try{
var test = this._collectionList[collectionIndex].find({_id:fk}).fetch();
if(test.length==1){
return true;
}
else{
return false;
}
}
catch(e){
throw new Meteor.Error("DB error", e.message);
}
}
if(Meteor.isServer){
try{
var test = this._collectionList[collectionIndex].find({_id:fk}).fetch();
if(test.length==1){
return callBack(null,true);
}
else{
return callBack(null,false);
}
}
catch(e){
throw new Meteor.Error("DB error", e.message)
}
}
}
然后:
try{
test = Meteor.call(this._collectionList[index]._prefix.replace(/\//g,"")+this._checkFKSuffix,document[element.entryName]);
}
catch(e){
throw new Meteor.Error("DB error",e.message)
}
如果有人能解释为什么服务器端需要回调,而客户端不需要回调,那就太好了!