当我尝试调用此oauth-1.0 API请求时,
const request = require('request');
const OAuth = require('oauth-1.0a');
const crypto = require('crypto');
function main(params) {
// Dependencies
// Initialize
const oauth = OAuth({
consumer: {
key: '****',
secret: '****'
},
signature_method: 'HMAC-SHA1',
hash_function(base_string, key) {
return crypto.createHmac('sha1', key).update(base_string).digest('base64');
}
});
// Note: The token is optional for some requests
const token = {
key: '****',
secret: '****'
};
const request_data = {
url: 'http://****/rest/V1/products/12345',
method: 'GET',
//data: { status: 'Hello Ladies + Gentlemen, a signed OAuth request!' }
};
return new Promise((resolve, reject) => {
request({
url: request_data.url,
method: request_data.method,
form: request_data.data,
headers: oauth.toHeader(oauth.authorize(request_data, token))
}, function (err, res, body) {
//console.log(res.body.name);
if (err){
reject({
statusCode: 500,
headers: { 'Content-Type': 'application/json' },
body: {'message': 'Error processing your request' },
});
} else {
resolve({
body: JSON.parse(body),
})
}
});
});
};
exports.main = main;
返回
承诺{<未决>}
未定义
但是当我只使用console.log(body)时,它会给出正确的结果
..................
........
....
.....
.....
.....
答案 0 :(得分:0)
实际上,它可以正确运行并返回Promise。
因此,如果您想从诺言中获取“数据”,则应使用here
就像本例中的MDN一样。
const promise1 = new Promise(function(resolve, reject) {
resolve('Success!');
});
promise1.then(function(value) {
console.log(value);
// expected output: "Success!"
});
希望有帮助!