0

I am trying to check whether a table entry exists in a database, but what I have so far always returns true even if there is no entry. What am I doing wrong?

Thank you all.

This will always 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;
    });
}
Program-Me-Rev
  • 6,184
  • 18
  • 58
  • 142
  • 2
    hello you can check this [link](https://stackoverflow.com/questions/8829102/check-if-table-exists-without-using-select-from) from s – Birante Oct 08 '18 at 11:03
  • I'm not familiar with node.js so this may be wrong but `SELECT EXISTS (SELECT ...)` will always return a value (0 or 1) so seems unlikely to fail the `row && row.length` test. You should just use the subquery as your query instead. – Nick Oct 08 '18 at 11:22

0 Answers0