Google Cloud Functions req/res 通过 node-fetch 实现 Promise
Google Cloud Functions req/res with Promise by node-fetch
我是 Javascript 的新手,我正在尝试在我使用的两项服务之间架起一座桥梁。目标只是获取主体或请求,对另一个服务进行承诺 api 调用,以响应该 api 调用的主体。我已经能够获取请求的主体并将其发送到服务,但我无法接收该响应并将该响应的主体作为函数的响应。请帮帮我。谢谢。
var moment = require('moment');
var CryptoJS = require("crypto-js");
const fetch = require('node-fetch');
var unixtime = moment().unix();
var apiUser = process.env.apiUser;
var secret = process.env.apiKey;
var url = process.env.url;
exports.test = (req, res) => {
var message = req.body;
message = JSON.stringify(message);
var body = "{\n \"ops\": [{\n \"conv_id\": \"679690\",\n \"type\": \"create\",\n \"obj\": \"task\",\n \"data\": message\n }]\n}\n"
body = body.replace(/message/ig, message);
var signature = CryptoJS.enc.Hex.stringify(CryptoJS.SHA1(unixtime + secret + body + secret));
function request1() {
return new Promise((resolve, reject) => {
var options = fetch(url+apiUser+'/'+unixtime+'/'+signature, {
method: 'post',
body: body,
headers: { 'Content-Type': 'application/json' },
});
options.then(res => {
var result = res.json;
console.log(result);
resolve(result);
})
.catch(() => { // if .then fails
console.log('Promise rejected');
let rejectMessage = 'Sorry, an error occurred.';
reject(rejectMessage); // Promise rejected
});
});
}
request1();
};
您可以轻松检索 result
对象,因为函数 request1
returns 是一个解决该对象的承诺,所以这应该有效:
request1().then((resultObject)=>{
//resultObject === result
return res.send(resultObject);
});
此外,res.json()
returns一个承诺,所以你应该做:
options.then(res => res.json()).then(result => {
console.log(result);
resolve(result);
})
我是 Javascript 的新手,我正在尝试在我使用的两项服务之间架起一座桥梁。目标只是获取主体或请求,对另一个服务进行承诺 api 调用,以响应该 api 调用的主体。我已经能够获取请求的主体并将其发送到服务,但我无法接收该响应并将该响应的主体作为函数的响应。请帮帮我。谢谢。
var moment = require('moment');
var CryptoJS = require("crypto-js");
const fetch = require('node-fetch');
var unixtime = moment().unix();
var apiUser = process.env.apiUser;
var secret = process.env.apiKey;
var url = process.env.url;
exports.test = (req, res) => {
var message = req.body;
message = JSON.stringify(message);
var body = "{\n \"ops\": [{\n \"conv_id\": \"679690\",\n \"type\": \"create\",\n \"obj\": \"task\",\n \"data\": message\n }]\n}\n"
body = body.replace(/message/ig, message);
var signature = CryptoJS.enc.Hex.stringify(CryptoJS.SHA1(unixtime + secret + body + secret));
function request1() {
return new Promise((resolve, reject) => {
var options = fetch(url+apiUser+'/'+unixtime+'/'+signature, {
method: 'post',
body: body,
headers: { 'Content-Type': 'application/json' },
});
options.then(res => {
var result = res.json;
console.log(result);
resolve(result);
})
.catch(() => { // if .then fails
console.log('Promise rejected');
let rejectMessage = 'Sorry, an error occurred.';
reject(rejectMessage); // Promise rejected
});
});
}
request1();
};
您可以轻松检索 result
对象,因为函数 request1
returns 是一个解决该对象的承诺,所以这应该有效:
request1().then((resultObject)=>{
//resultObject === result
return res.send(resultObject);
});
此外,res.json()
returns一个承诺,所以你应该做:
options.then(res => res.json()).then(result => {
console.log(result);
resolve(result);
})