Node.js无法在循环内运行mysql查询

时间:2018-09-02 13:17:01

标签: javascript mysql node.js

我在mysql中有两个表,想根据另一个表的结果查询一个表,所以我写了一个函数

export function getLocations(req, res) {
const database = new Database();
database.query('select * from districts')
    .then(rows => {
        let appData = [];
        rows.forEach(row => {
            const new_database = new Database();
            new_database.query(`SELECT locations.id,locations.name, IF(subscriptions.id IS NULL,0,1) as subscribed 
            FROM locations 
            LEFT JOIN subscriptions 
            ON (subscriptions.location_id = locations.id AND subscriptions.user_id=1)
            WHERE locations.district=?`,row.id)
            .then(sub_rows => {
                let district=row;
                district["locations"]=sub_rows;
                appData.push(district);
                new_database.close();
            }, err => {
                return new_database.close().then(() => { throw err; })
            })
            .catch(err => {
                console.log(err);
                res.status(500).json("Database Error");
            })

        });
        res.status(200).json(appData);  //final result here
        database.close()
    }, err => {
        return database.close().then(() => { throw err; })
    })
    .catch(err => {
        console.log(err);
        res.status(500).json("Database Error");
    })
}

在这里,我想基于第一个查询的每一行运行第二个查询。 我得到一个空数组作为结果。我的第一个查询正确执行,并且我记录了所有行都已返回。可能是什么问题?

2 个答案:

答案 0 :(得分:0)

您可以通过使其异步来使其工作

rows.forEach(async row => {
        const new_database = new Database();
        await new_database.query(`SELECT locations.id,locations.name, IF(subscriptions.id IS NULL,0,1) as subscribed 
        FROM locations 
        LEFT JOIN subscriptions 
        ON (subscriptions.location_id = locations.id AND subscriptions.user_id=1)
        WHERE locations.district=?`,row.id)
        .then(sub_rows => {
            let district=row;
            district["locations"]=sub_rows;
            appData.push(district);
            new_database.close();
        }, err => {
            return new_database.close().then(() => { throw err; })
        })
        .catch(err => {
            console.log(err);
            res.status(500).json("Database Error");
        })

});

您正在执行的操作是I / O,而JS是单线程的。从外行的角度来看,这意味着它不会等待,它将在您将要发出请求的地方迭代您的循环,而在请求处理期间会有一些等待。您需要告诉JS此事件是异步的。为此,您需要使用async / await

指南

forEach async/await

async/await MDN documentation

答案 1 :(得分:0)

我的机器没有环境。它可能有一些错误,但是如果有的话,您可以修复它,请看以下逻辑

export function getLocations(req, res) {
const database = new Database();
const promises=[];
database.query('select * from districts')
    .then(rows => {
        let appData = [];
        rows.forEach(row => {
        promises.push(getAnotherQuery(row));
        });
        database.close()
    }, err => {
        return database.close().then(() => { throw err; })
    })
    .catch(err => {
        console.log(err);
        res.status(500).json("Database Error");
    })
    return Promise.all(promises).then(result)=>  res.status(200).json(result);  //final result here

}
getAnotherQuery=(row)=>{
return new Promise((resolve,reject)=>{
const new_database = new Database();
           const appData=[]
            new_database.query(`SELECT locations.id,locations.name, IF(subscriptions.id IS NULL,0,1) as subscribed 
            FROM locations 
            LEFT JOIN subscriptions 
            ON (subscriptions.location_id = locations.id AND subscriptions.user_id=1)
            WHERE locations.district=?`,row.id)
            .then(sub_rows => {
                let district=row;
                district["locations"]=sub_rows;
                appData.push(district);
                new_database.close();
                resolve(appData);
            }, err => {
                return new_database.close().then(() => { throw err; })
            })
            .catch(err => {
                console.log(err);
                res.status(500).json("Database Error");
            })
            });
}