如何检查数据库条目(表行)是否已存在

时间:2018-10-08 10:58:37

标签: javascript mysql node.js database

我正在尝试检查数据库中是否存在表条目,但是到目前为止,即使没有条目,我也总是返回true。我究竟做错了什么?

谢谢大家。

这将始终console.log >>>> true

let myPersLocalEntityExistsPromise = function (localEntityGUID) {
    return new Promise(function (resolve, reject) {

        let myEntityExists = true;

        const client = myDb.mySQLDbLocalConnection();


        let stmt = "SELECT EXISTS(SELECT 1 FROM MY_ENTITY_TABLE WHERE LOCAL_MY_ENTITY_GUID = ?)";

        let todo = [
        localEntityGUID
        ];

        client.query(stmt, todo, function (err, row) {
            if (err) {
                return ("myPersLocalEntityExists: " + err.message);
            } else {
                if (row && row.length) {
                    console.log(localEntityGUID + ' Case row was found!');
                } else {
                    myEntityExists = false;
                    console.log(localEntityGUID + ' Case row was NOT found!');
                }
            }
        });

        client.end(function (err) {
            if (err) {
                console.log('ERRR');
            }
        });

        if (myEntityExists) {
            resolve(myEntityExists);
        } else {
            reject(myEntityExists);
        }
    })
}

function myPersLocalEntityExists(localEntityGUID, callback) {
    let responsePromises = []
    responsePromises.push(myPersLocalEntityExistsPromise(localEntityGUID))

    Promise.all(responsePromises).then(fromResolve => {
        console.log(">>>> " + fromResolve);
        return fromResolve;
    }).catch(fromReject => {
        console.log(">>>> " + fromReject);
        return fromReject;
    });
}

0 个答案:

没有答案