如何在 Parse Cloud Code 函数内使用 axios 发送 HTTP 请求?

huangapple go评论52阅读模式
英文:

How to make a http request with axios from inside Parse Cloud Code function?

问题

我尝试在Parse Cloud Code函数内发出请求。我的尝试如下:

const axios = require('axios').default;

Parse.Cloud.define("loginSchool", async (request) => {
  test = await axios({
    method: 'get',
    url: 'https://google.com'
  });
  return(test);
});

我可以使用curl调用该函数,但结果只返回Bad Gateway

我正在使用Parse版本5.2.3的Back4App。

在Cloud Code函数内正确使用axios进行HTTP请求的方法是什么?

英文:

I'm trying to make a request from inside a Parse Cloud Code function. My attempt looks likes this:

const axios = require('axios').default;

Parse.Cloud.define("loginSchool", async (request) => {
  test = await axios({
    method: 'get',
    url: 'https://google.com'
  });
  return(test);
});

I can call the function via curl but it only returns Bad Gateway as result.

I'm using Back4App with Parse Version 5.2.3.

What is the correct way to use axios inside a cloud code function for making a http request?

答案1

得分: 0

axios不会默认包含在Parse云代码中,所以您需要手动安装它。例如,如果您在本地开发,请在云文件夹中运行 npm install axios,然后部署云文件夹,包括 node_modules 文件夹。

另外,您也可以尝试下载axios模块,然后通过云代码仪表板上传它。但对我来说,设置本地的parse_server 对我有很大帮助,因为我能更快地查看日志,从而确定问题。

英文:

Found the problem by myself but maybe it helps someone else.

axios is not included as dependency by default inside parse cloud code. Therefore you need to install it. e.g. if you develop locally run npm install axios in the cloud folder and afterwards deploy the cloud folder, including the node_modules folder.

Alternativly I imagine you could download the axios module and upload it via the Dashboard for Cloud Code. But for me setting up a local parse_server helped me a lot because I could see the logs more quickly and therefore determine the problem.

huangapple
  • 本文由 发表于 2023年3月4日 04:34:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631654.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定