簡體   English   中英

Nodejs Promise無法解析/未返回數據

[英]Nodejs Promise not resolving/no data returned

我有一個promise函數,該函數會進行兩次請求調用,並在第二次調用完成后解決。 第二個解析調用也取決於第一個調用中的數據。 但是在then函數中,變量返回值我為null。 任何幫助,將不勝感激。

編輯:secResp.body具有正確的數據,它不是null

const express = require('express');
const request = require('request');
const app = express();
const port = process.env.PORT || 5000;

// console.log that your server is up and running
app.listen(port, () => console.log(`Listening on port ${port}`));

app.get('/api/currentMatch/:name', function(req, res, next){
    getPlayersInMatch(req.params.name).then(function(participants){
        //this is null
        console.log(participants);
    }).catch(function (err) {
     console.log(err);
    });
})

function getPlayersInMatch(name){
    return new Promise(function(resolve, reject){
        request.get({
            url: api_url
        }, function(firstErr, firstResp, body){
            if(firstResp.StatusCode != 200){
                reject(firstErr);
            }
            var accountId = JSON.parse(firstResp.body).id;
            request.get({
                url: api_url2 + 'accountId=' + accountId
            }, function(secErr, secResp, body){
                if(secResp.StatusCode != 200){
                    reject(secErr);
                }
                //this is not null, this is an array
                var participants = JSON.parse(secResp.body).participants;
                resolve(participants);
            });
        });
    });
}

我將您的代碼重寫為request-promise 我發現您的代碼的主要問題是它太復雜了。 簡化事情可以更輕松地發現您做錯了什么。

const rp = require('request-promise')
const app = express();
const port = process.env.PORT || 5000;

// console.log that your server is up and running
app.listen(port, () => console.log(`Listening on port ${port}`));

app.get('/api/currentMatch/:name', function(req, res, next){
    getPlayersInMatch(req.params.name)
        .then(console.log)
        .catch(console.error)
})

const getPlayersInMatch = async name => {
    const id = await rp(api_url)
        .then(res => JSON.parse(res.body).id)

    const participants = await rp(api_url2 + 'accountId=' + accountId)
        .then(res => JSON.parse(res.body).participants)

    return { id, participants }
}
function getPlayersInMatch(name){
    return new Promise(async function(resolve, reject){
        return await request.get({
            url: api_url
        }, function(firstErr, firstResp, body){
            if(firstResp.StatusCode != 200){
                reject(firstErr);
            }
            var accountId = JSON.parse(firstResp.body).id;
            request.get({
                url: api_url2 + 'accountId=' + accountId
            }, function(secErr, secResp, body){
                if(secResp.StatusCode != 200){
                    reject(secErr);
                }
                //this is an array
                var participants = JSON.parse(secResp.body).participants;
                resolve(participants);
            });
        });
    });
}

試試吧。

一,您應該返回請求結果或對象。 第二,您應該使用異步等待,因為它不等待異步回調中的數據請求。

在您的代碼中,很難找出哪個步驟出錯。

我認為最好像這樣包裝請求模塊:

/**
 * Mapping knowing error_code
 * @param {Number} error_code 
 * @param {String} msg 
 */
function error_def(error_code, msg) {
    let status, message;
    switch(error_code){
        case 400:
        case 401:
        case 402:
        case 403:            
        case 1000:
        case 1001:
        case 1002:
        case 1003:
        case 1005:
            status = error_code;
            message = msg;
            break;
        default:
            status =  2000;
            message = 'Undefined Error'
    }
    return {status: status, msg: message};
}

/**
 * Generate error message
 * @param {String} tag
 * @param {Number} error_code
 * @param {String} msg
 */
function gen_error_func(tag = null) {    
    return function(error_code, msg = null) {
        return {tag: tag, error_message: error_def(error_code, msg)}
    }
}

/**
 * Wrap the request and return interesting keys
 * @param {String} tag 
 * @param {Object} req_opt 
 * @param {Array} interesting_resp
 * @return {Object}
 */

function req_wrap(tag = null, req_opt, interesting_resp = null){
    return new Promise((resolve, reject) => {
        let gen_error = gen_error_func(tag)

        if (!req_opt.url) {
            reject(gen_error(1000, 'missing url'));
        }

        let option = {
            url: req_opt.url,
            method: (req_opt.method)? req_opt.method: 'GET',
        }

        request(option, function(error, response, body) {
            if(error) {
                reject(gen_error(1001, error));
                return;
            }

            if (!response) {
                reject(gen_error(1005, 'response is undefine, maybe wrong url!'))
                return;
            }

            if (response.statusCode >= 400) {
                //http level error
                reject(gen_error(response.statusCode, body));
            }else {
                let result = {};
                let body_json;
                try {
                    body_json = JSON.parse(body);
                }catch(e) {
                    reject(gen_error(1002, `Unknow response: ${body}`))
                }
                if (interesting_resp) {
                    interesting_resp.map(key => {
                        if (body_json[key] == undefined) {
                            reject(gen_error(1003, `In ${body_json}, undefined ${key}`))
                            return;
                        }
                        result[key] = body_json[key];
                    })
                }else {
                    result = body_json;
                }
                resolve(result);
            }
        })
    })
}
  • req_wrap:
    • 參數
      • tag :顯示您定義的請求,例如:“首次連接”
      • req_opt :請求選項
      • funny_resp :您感興趣的鍵

注意:在req_wrap中,如果它在響應中找不到有趣的鍵(undefined或null),則該函數將拒絕。 有必要檢查您要求的服務器規格!

您可以重寫代碼:

function getPlayersInMatch(name){
    return new Promise(function(resolve, reject){
        req_wrap('first_conn', {url: api_url}, ['id']).then(result => {            
            req_wrap('second_conn', {url: api_url2 + 'accountId=' + result.id}, ['participants']).then(result => {
                resolve(result.participants)
            }).catch(error => {
                reject(error)
            })
        }).catch(error => {
            reject(error)
        })
    });
}

現在,更清楚地檢查哪一步是錯誤的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM