mercredi 23 septembre 2020

Can anyone tell me why this always executes the "else" part no matter the result of the "if" condition [node js]

I have this route;

    router.get('/:specimenID', (req, res) => {
    const sampleID = req.params.specimenID;
    sql.connect(con, () => {
        const request = new sql.Request();
        const isValidSpecimenID = `SELECT CASE WHEN EXISTS
        (SELECT TOP 1 1 FROM Interface WHERE
             Interface.SpecimenID =`+ sampleID +`) THEN 1 ELSE 0 END`;
        const sqlQuery = `Select Interface.SpecimenID,
        Interface.Result,Interface.Range,Interface.ServerTime,
        Interface.AnalyzerCode from Interface 
        left join LabAnalyzerElements 
        on Interface.ElementID=LabAnalyzerElements.MappedElement 
        where Interface.AnalyzerCode = LabAnalyzerElements.AnalyzerID and Interface.SpecimenID =` + sampleID;
        request.query(sqlQuery)
        .then(results => {
            if (isValidSpecimenID == 1) {
                res.status(200).json({
                LISResults : results.recordset
            }); 
        }else {
                res.status(404).json({
                    Message: "invalid Specimen ID"
                });
            }
        })
        .catch(err => {
            res.status(400).json({
                message: err.message
            })
        })
    });
});

I want the response to be based on either the specimenID exist or Not but I always get an "invalid Specimen ID" response of the "else" block and I cant figure out why..

I tried the isValidSpecimenID variable query, in MSSQL Management Studio and it does return '0' if specimenID is not in the table and vice versa

It is a nodejs API using mssql and express server.

Any suggestions will be appreciated.

Aucun commentaire:

Enregistrer un commentaire