Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.JS-返回承诺给出“未定义”_Node.js_Promise_Return_Oauth 1.0a - Fatal编程技术网

Node.JS-返回承诺给出“未定义”

Node.JS-返回承诺给出“未定义”,node.js,promise,return,oauth-1.0a,Node.js,Promise,Return,Oauth 1.0a,当我尝试调用这个oauth-1.0API请求时 const request = require('request'); const OAuth = require('oauth-1.0a'); const crypto = require('crypto'); function main(params) { // Dependencies // Initialize const oauth = OAuth({ consumer: {

当我尝试调用这个oauth-1.0API请求时

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.logbody时,它给出了正确的结果


…事实上,它工作正常,并返回承诺

所以,如果您想从promise获取数据,您应该使用

就像MDN中的这个例子一样

const promise1 = new Promise(function(resolve, reject) {
  resolve('Success!');
});

promise1.then(function(value) {
  console.log(value);
  // expected output: "Success!"
});

希望有帮助

您必须等待承诺在.then回调中得到解决。请检查:我遵循的是IBM Cloud Functions Weather模板的布局,该模板与我的布局几乎完全相同,只是我的布局是oauth,而天气不是。我只是不知道为什么它能工作,但我的不行。不过我不想使用console.log。当我做“返回值”的时候,它仍然没有给出任何东西。你能把代码贴在你处理承诺的地方吗?我会把整个事情贴出来。我举的例子是:,它工作得很好。我看不到任何可能导致问题的东西。但仍然不清楚如何处理主函数的调用。