如何通过 http 请求在 Parse Server 中创建一个 returns 东西的云函数

How to create a cloud function in Parse Server that returns something via a http request

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

创建一个调用 http 请求的云函数,然后调用该云函数 returns 来自 http 请求的响应,正确的方法是什么,因为我在使用这种方法时遇到错误,我认为我以错误的方式使用了承诺的概念。

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 数据你可以简单地在响应中发送它 object.Also 你应该在你的块被执行之后调用 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");
  });



});