Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 正在从客户端上的Meteor.call获取未定义,但服务器上的变量包含数据_Javascript_Http_Meteor - Fatal编程技术网

Javascript 正在从客户端上的Meteor.call获取未定义,但服务器上的变量包含数据

Javascript 正在从客户端上的Meteor.call获取未定义,但服务器上的变量包含数据,javascript,http,meteor,Javascript,Http,Meteor,我在服务器上有这样一个函数 Meteor.methods({ "functionName": function(data) { //calculations, http requests etc. console.log(variable); //shows variable properly return variable; } }); 当我在服务器上执行console.log(变量)时,我可以看到数据 现在我在客户端执行Meteor.call: Meteor.call('function

我在服务器上有这样一个函数

Meteor.methods({
"functionName": function(data) {
//calculations, http requests etc.

console.log(variable); //shows variable properly
return variable;
}
});
当我在服务器上执行
console.log(变量)
时,我可以看到数据

现在我在客户端执行
Meteor.call

Meteor.call('functionName', data, function(err, res) {
console.log(res); //shows undefined
});
这里怎么了?为什么我不能在客户端上使用响应变量

编辑:

我已经为此挣扎了几个小时,我将错误缩小到一个,
HTTP.get()
function。现在看起来是这样的:

//some calculations
var variable = HTTP.get('url');
return variable.data; //should return an object
//some calculations
function promise() {
  var variable = HTTP.get('url');
  resolve('Done.');
}

promise().then(function() {
  console.log(variable); //doesn't even work on server this way
  return variable;
});
我试着用承诺来包装它,但后来没有成功。看起来是这样的:

//some calculations
var variable = HTTP.get('url');
return variable.data; //should return an object
//some calculations
function promise() {
  var variable = HTTP.get('url');
  resolve('Done.');
}

promise().then(function() {
  console.log(variable); //doesn't even work on server this way
  return variable;
});

根据这些评论和您的问题,我猜您的return语句发生在HTTP请求的异步回调中。您可以使用或使这些请求同步,并获取有效的返回语句。

此问题是由于http的同步调用引起的。 这里真正发生的事情不是等待http调用响应。
您应该使用Meteor.wrapAsync

可能该变量不可序列化。请提供有关它的更多详细信息,并检查
err
。Error不返回任何内容。你能详细说明一下“不可序列化”吗?我不熟悉这个术语。变量集是一个字符串,如果它改变了什么的话。你确定这个值确实是从方法同步返回的吗?如果它在异步回调中,您将需要使用承诺或包装异步调用。我很确定这里出现了
http请求
,请尝试返回承诺。