【问题标题】:Meteor 1.6 make synchronous call both client and server sideMeteor 1.6 在客户端和服务器端进行同步调用
【发布时间】:2018-05-31 13:25:20
【问题描述】:

我目前正在开发一个集合管理器,它也管理外键。 它还会生成一些表单,我无法检查外键并正确回调客户端。

我正在使用来自流星的 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 关键字,但它只是给了我错误......)

【问题讨论】:

    标签: javascript meteor


    【解决方案1】:

    您的客户端函数总是需要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 工作原理的更多信息),而您的客户端将始终以回调样式接收结果或错误。因此,在调用方法时在客户端上使用某种异步/等待样式模式的想法可能无法实现。

    【讨论】:

    • 这实际上是我的问题,我怎样才能在同步功能中将结果发送给客户端?您提供的解决方案在服务器上为“真”时将未定义的结果发送给客户端。
    • 我已经阅读了它,但它并没有真正回答我的问题。我找到了解决方案。谢谢你的回答。
    【解决方案2】:

    我实际上找到了一个解决方案,但我不确定它为什么会起作用,如果有人可以清楚地解释:

    我只需要像这样制作一个“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)
    }
    

    如果有人能解释为什么服务器端需要回调而不是客户端需要回调,那就太好了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-25
      • 1970-01-01
      • 1970-01-01
      • 2018-02-03
      • 2014-02-18
      • 1970-01-01
      • 2016-10-16
      相关资源
      最近更新 更多