Javascript 使用Firebase函数使用外部api
我试图使用带有Firebase函数的外部api,但当我使用OPTIONS时,当我使用GET正常工作时,它会出现超时错误。 我不想使用const request=require('request')或var rp=require('request-promise'),因为它们已过时。 可能有什么问题,我在等待同事们的帮助Javascript 使用Firebase函数使用外部api,javascript,node.js,firebase,google-cloud-functions,Javascript,Node.js,Firebase,Google Cloud Functions,我试图使用带有Firebase函数的外部api,但当我使用OPTIONS时,当我使用GET正常工作时,它会出现超时错误。 我不想使用const request=require('request')或var rp=require('request-promise'),因为它们已过时。 可能有什么问题,我在等待同事们的帮助 const express = require('express'); const cors = require('cors'); const app = express();
const express = require('express');
const cors = require('cors');
const app = express();
// Permitir solicitações de origem cruzada automaticamente
app.use(cors({
origin: true
}));
//app.use(cors());
app.get('/criarcliente', (req, res) => {
let uri = "https://api.iugu.com/v1/customers?api_token=<mytoken>";
let headers = {
'Content-Type': 'application/json'
}
let body = {
custom_variables: [{
name: 'fantasia',
value: 'Dolci Technology'
}, {
name: 'vendedor',
value: ''
}],
email: 'teste1@teste1.com',
name: 'John Dolci',
phone: 9999999,
phone_prefix: 66,
cpf_cnpj: '00000000000',
cc_emails: 'test@test.com',
zip_code: '78520000',
number: '49',
street: 'Name Street',
city: 'Guarantã do Norte',
state: 'MT',
district: 'Jardim Araguaia'
}
var options = {
method: 'POST',
uri: uri,
body: body,
headers: headers,
json: true
};
const https = require('https');
var req = https.request(options, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
var result = JSON.parse(data);
res.send(result);
});
console.log("aqui");
}).on("error", (err) => {
console.log("Error: " + err.message);
});
}); ```
const express=require('express');
const cors=需要(“cors”);
常量app=express();
//自动请求许可证
应用程序使用(cors)({
来源:真实
}));
//应用程序使用(cors());
app.get('/criarcliente',(req,res)=>{
让uri=”https://api.iugu.com/v1/customers?api_token=";
让标题={
“内容类型”:“应用程序/json”
}
让主体={
自定义变量:[{
名字:'幻想曲',
价值:“多尔奇科技”
}, {
名称:“卖方”,
值:“”
}],
电邮:'teste1@teste1.com',
姓名:“约翰·多尔奇”,
电话:9999999,
电话号码前缀:66,
cpf_cnpj:'00000000000',
抄送电子邮件:'test@test.com',
邮政编码:“78520000”,
编号:'49',
街道:“名字街”,
城市:“北方卫兵”,
声明:“MT”,
地区:'Jardim Araguaia'
}
变量选项={
方法:“POST”,
uri:uri,
身体:身体,,
标题:标题,
json:true
};
常量https=require('https');
var req=https.请求(选项,(响应)=>{
让数据=“”;
在('数据',(块)=>{
数据+=块;
});
分别在('结束',()=>{
var result=JSON.parse(数据);
res.send(结果);
});
控制台日志(“AQI”);
}).on(“错误”,(错误)=>{
日志(“错误:+err.message”);
});
}); ```
对于你的情况,我想指出两点。首先,您需要确认您在账单中使用了Blaze计划。正如官方澄清的那样,如果您没有并且正在尝试使用非谷歌所有的服务,您将无法这样做,这将导致错误
第二点是在应用程序中使用库。我认为它是Node.js中最常用的一个,它允许您访问云功能中的第三方应用程序——我想说它也很容易使用。您应该可以找到一个将其与第三方API结合使用的完整示例
总而言之,请查看您的定价,因为这是最常见的问题,请尝试使用
axios
,以确认您可以避免错误。感谢您的回答,它已经帮了我很多忙,我正在使用Blaze plan。使用GET,我通常只能在PUT和POST方面遇到问题,您告诉我您正在使用GET的这个示例,可能您没有使用POST或PUT的示例?Hi@LucasDolci,您可以使用POST
检查或查找示例。请,如果你认为我的答案对你有帮助,考虑接受它,这样社区就可以确认这个案子得到了妥善的解决。