gpt4 book ai didi

javascript - 如何在 Parse Server 中创建一个通过 http 请求返回内容的云函数

转载 作者:行者123 更新时间:2023-12-02 14:12:10 27 4
gpt4 key购买 nike

我正在尝试在 Parse 中执行以下操作:

创建一个调用http请求的云函数,然后云函数从http请求返回此响应,正确的方法是什么,因为我在使用这种方法时遇到错误,我认为我正在使用这个概念以错误的方式 promise 。

Parse.Cloud.define('test_function', function(req, res){
var myData = {}

Parse.Cloud.httpRequest({
method: 'POST',
url: 'http://dummyurl',
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
body: {
some_data : "test_data"
}
}).then(function(httpResponse) {
console.log(httpResponse.text);
myData = httpResponse.data;

}, function(httpResponse) {
console.error('Request failed with ' + httpResponse.status);
res.error("Request failed");
});


res.success(myData);
});

最佳答案

由于您要返回 JSON 数据,因此您可以简单地将其发送到响应对象中。此外,您应该在 block 执行后而不是执行后立即调用 response.success 所以在您的情况下,您的代码应如下所示:

Parse.Cloud.define('test_function', function(req, res) {
var myData = {}

Parse.Cloud.httpRequest({
method: 'POST',
url: 'http://dummyurl',
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
body: {
some_data: "test_data"
}
}).then(function(httpResponse) {
console.log(httpResponse.text);
myData = httpResponse.data;
res.success(myData); // this should be called in here!

}, function(httpResponse) {
console.error('Request failed with ' + httpResponse.status);
res.error("Request failed");
});



});

关于javascript - 如何在 Parse Server 中创建一个通过 http 请求返回内容的云函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39438498/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com