1
someMethod = new ValidatedMethod({
    name: 'someMethodName',
    validate: new SimpleSchema({
        subId: {type: String, min:1},
        planId: {type: String}        
    }).validator(),
    async run(params){
        try{
                     //params is undefined
        } 

    }
});

使用async run(params)原因(params似乎undefined上下文切换到Global上下文)。删除async工作正常(除了我不能在方法体中使用 await 显然)。为什么会这样,我怎么还能在 a 中使用 await ValidatedMethod

注意1:我像这样从客户端调用该方法——如果我尝试使用常规Meteor.methods({})定义,则会得到相同的结果。我正在Meteor.apply从客户端调用使用的方法

ClientHelpers.callWithPromise = function(methodName, methodArgs){
  //methodArgs must be an array
  return new Promise(function(resolve, reject){
    Meteor.apply(methodName, methodArgs, {wait:true}, function(error, result){
      if (error){
        reject(error);
      } 
      console.log(result);
      resolve(result);
    });
  });
}

然后,调用客户端(我确定paramsObject是正确的):

var myResult = await ClientHelpers.callWithPromise('someMethodName', [paramsObject]);

注意 2:我还追踪到了 Meteor.apply 的内部,实际上它paramsObject在调试会话中发送了 over DDP:

  // Sends the DDP stringification of the given message object
  _send(obj) {
    this._stream.send(DDPCommon.stringifyDDP(obj));
  }

非常感谢您的任何见解。

4

0 回答 0