在Meteor.method中调用函数将返回未定义 [英] Calling a function in Meteor.method returns undefined

查看:58
本文介绍了在Meteor.method中调用函数将返回未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去几天,我一直在尝试从Meteor方法获取返回对象.每次执行此操作时,客户端上都会显示 undefined .

I've been trying for the past few days to get a return object from a Meteor method. Every time I do this I get undefined on the client.

Meteor.methods({
 'CORSTest' : function() {
  let url = "www.theverge.com/2017/4/13/15270854/nasa-enceladus-ocean-hydrothermal-vents-alien-life-conditions-cassini-saturn";
   og(url, function(err, meta){
    if(err){
     console.log(err);
     return "Error";
    } else {
     console.log(meta);
     // Returns the correct Object on the server
     return meta;
    }
  })
 },
})

我为此一直发疯.尝试所有不同的变量和语法,我似乎无法正常工作.

I've been going crazy over this. Trying all different variables and syntax and I can't seem to get this to work.

任何人都可以提供的帮助将是不可思议的.

Any help anyone can provide would be incredible.

推荐答案

这是一个非常常见的流星问题.您正在方法内部调用异步函数.您的 return 语句会将值从您的匿名函数返回到方法范围,而将 not 从服务器方法返回到客户端.您可以遵循几种模式来解决此问题.您可以使用承诺,也可以包装匿名函数调用并进行它与 Meteor.wrapAsync 同步.例如:

This is a very common Meteor question. You are calling an asynchronous function inside your method. Your return statements are returning values from your anonymous function to the method scope, not from the server method to the client. There are several patterns you can follow to get around this. You can use promises or you can wrap your anonymous function call and make it synchronous with Meteor.wrapAsync. For example:

Meteor.methods({
  CORSTest() {
    const url = "www.theverge.com/2017/4/13/15270854/nasa-enceladus-ocean-hydrothermal-vents-alien-life-conditions-cassini-saturn";
    const syncFun = Meteor.wrapAsync(og);
    return syncFun(url);
  }
})

这篇关于在Meteor.method中调用函数将返回未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆