问题描述
我有两个函数,我从计数"中的"http"返回承诺中调用"http".我想在计数"中使用"http"的返回值.我现在收到的是未定义!我想念的是什么?
I have those two functions where i call "http" from "Count" the "http" return promise. I want to use the return value of "http" in "Count". What I receive now is Undefined !!!What I'm missing ?
计数功能:
Parse.Cloud.define('count', function(request, response) {
var query = new Parse.Query('MyS');
query.equalTo("Notify", true);
query.notEqualTo ("MainEventCode", '5');
query.find({
success: function(results) {
Parse.Cloud.run('http', {params : results}).then(
function(httpResponse) {
console.log('httpResponse is : ' + httpResponse.length);
response.success('Done !');
}, function(error) {
console.error(error);
});
},
error: function(error) {
response.error(error);
}
});
});
http函数:
Parse.Cloud.define('http', function(request, response) {
var query = new Parse.Query(Parse.Installation);
.
.
.
}
推荐答案
我认为您要问的是如何使用外部可调用的云函数作为更大的云过程中的一个步骤.操作方法如下:(@ paolobueno本质上是正确的,在细节上只有几个错误).
I think what you're asking is how to use an externally callable cloud function as a step in a bigger cloud procedure. Here's how to do it: (@paolobueno has it essentially correct, with only a couple mistakes in the details).
首先,让我们将"http"云函数转换为常规JS函数.我们需要做的就是排除request
和response
对象. (@paolobueno有一个使用underscorejs的好主意,但是我不会在这里,因为这是另一个需要学习的新东西.)
First, let's convert that 'http' cloud function to a regular JS function. All we need to do is factor out the request
and response
objects. (@paolobueno has a very good idea to use underscorejs, but I won't here because its another new thing to learn).
// for each object passed in objects, make an http request
// return a promise to complete all of these requests
function makeRequestsWithObjects(objects) {
// underscorejs map() function would make this an almost one-liner
var promises = [];
for (var i = 0; i < objects.length; i++) {
var object = objects[i];
promises.push(makeRequestWithObject(object));
}
return Parse.Promise.when(promises);
};
// return a promise to do just one http request
function makeRequestWithObject(object) {
var url = 'http://185.xxxxxxx'+ object +'&languagePath=en';
return Parse.Cloud.httpRequest({ url:url });
}
您似乎想要更新的云功能-而不是使用来自客户端的参数-首先进行查询并将该查询的结果用作http调用函数的参数.这是这样做的方法. (再次,使用@paolobueno的出色实践来分解成返回诺言的函数...)
It looks like you want the updated cloud function -- rather than use params from the client -- to first make a query and use the results of that query as parameters to the http calling function. Here's how to do that. (Again, using @paolobueno's EXCELLENT practice of factoring into promise-returning functions...)
// return a promise to find MyS instances
function findMyS() {
var query = new Parse.Query('MyS');
query.equalTo("Notify", true);
query.notEqualTo ("MainEventCode", '5');
return query.find();
}
现在,我们拥有实现清晰,简单的公共功能所需的一切...
Now we have everything needed to make a clear, simple public function...
Parse.Cloud.define('count', function(request, response) {
findMyS().then(function(objects) {
return makeRequestsWithObjects(objects);
}).then(function(result) {
response.success(result);
} , function(error) {
response.error(error);
});
});
这篇关于在另一个功能中请求功能响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!