在箭头函数外使用变量

时间:2018-09-19 16:54:40

标签: node.js

我正在尝试创建一个库存系统,我想用不同表的数据来扩展数据。所以我的方法是这样:

router.get('/getArticles', (req, res) =>
{
    const logger = req.app.get('loggerHandle');
    const sql = req.app.get('sqlHandle');
    res.setHeader('Content-Type', 'application/json');
    sql.query('SELECT * FROM article', (arterr, artrows) =>
    {
        if(arterr)
        {
            logger.warn(arterr.stack);
            res.send(`{ "Error": { "Code": "${arterr.code}", "Fatal": "${arterr.fatal}", "SQL": "${arterr.sql}", "State": "${arterr.sqlState}", "Message": "${arterr.sqlMessage}" } }`);
        }
        else
        {
            sql.query('SELECT * FROM category', (caterr, catrows) =>
            {
                if(caterr)
                {
                    logger.warn(arterr.stack);
                    res.send(`{ "Error": { "Code": "${arterr.code}", "Fatal": "${arterr.fatal}", "SQL": "${arterr.sql}", "State": "${arterr.sqlState}", "Message": "${arterr.sqlMessage}" } }`);
                }
                else
                {
                    for(let i = 0; i < artrows.length; i++)
                    {
                        const currentCatID = catrows[artrows[i].CategoryID - 1];
                        artrows[i].CategoryID = currentCatID; // Var is okay, category info added
                    }
                }
                return artrows;
            });
            res.send(artrows); // Var not okay, category info is missing
        }
    });
    // ToDo
});

那么,我在做什么错了?

0 个答案:

没有答案