LEFT OUTER JOIN未返回任何结果

时间:2018-12-03 04:27:04

标签: node.js postgresql node-postgres

我有一个准备好的查询,正在研究中。此查询可在Psequel和PgAdmin中使用,但不适用于我的后端(node js)我正在与node js一起使用的postgresql库是node postgres  或'pg'https://node-postgres.com/ https://www.npmjs.com/package/pg我觉得很奇怪,它可以与其他应用程序一起正常工作,但不能在我的后端使用。请查看以下所提供的信息。我通过检查'1'值来检查用户是否为朋友,然后基于该值返回true或false(开放给建议),之后我将通过执行第二个查询来查找用户锻炼程序(无效),我希望获得的行与我正在使用的其他应用程序一样,都与用户相关联。该查询的实际结果是'[]'

getRoutine(username, user_id) {
    return new Promise(function (resolve, reject) {
        const client = new pg.Client(connectionString);
        client.connect()
            .then(() => console.log('connected'))
            .catch(err => console.error('connection error', err.stack))
        const text = "SELECT EXISTS(SELECT relationship_status FROM relationships FULL OUTER JOIN users ON users.user_id = relationships.user_one_id OR users.user_id = relationships.user_two_id WHERE users.username = $1 OR users.user_id = $2 AND relationships.relationship_status = 1)"
        const values = [username, user_id]
        const query = client.query(text, values)
            .then(res => {
                if (res.rows[0].exists === true) {
                    // find way to do this in single query
                    const text = "SELECT routine.workout, routine.personal_record, routine.workout_id, routine.upload_date FROM routine LEFT JOIN users ON users.user_id = routine.owner_id  WHERE users.username = $1"
                    const values = [username]
                    const query = client.query(text, values)
                        .then(data => {
                            console.log(data.fields)
                            resolve(data.rows)
                        })
                        .catch(error => {
                            console.log(error)
                            reject();
                        })
                } else {
                    reject();
                }
            })
            .catch(error => {
                console.log(error)
                reject();
            })
    });
}

0 个答案:

没有答案