0

I have the following codes :

app.post("/login/auth", (req, res) => {
    (async function() {
        let NikitaBellucci = (await auth.login(req, db, crypto))[0];
        res.send(NikitaBellucci);
    })();
});

and

exports.login = (req, db, crypto) => {
    pro = new Promise((resolve,reject) => {
        let pseudo = req.body.pseudo;
        let password = crypto.createHmac('sha256', req.body.password)
                       .update('jojofags suck')
                       .digest('hex');
        let query = "SELECT * FROM users WHERE users.pseudo = ? AND users.password = ? LIMIT 1";
        db.query(query, [pseudo, password], function (err, result) {
            if (err) throw err; // GESTION D'ERREURS
            result.isAdministrator = function() {
                if(this.role <= 90) { return true; } else { return false; }
            }
            resolve(result);
        });
    })

    return pro.then((val) => {
        console.log(val);
        return val;
    })
}

On console.log(val);, I can see the previously added method to my object. But when returning it to my main file, method "disappear", how to avoid that?

thank you

1
  • what exactly is the point of attaching that function to a temporary result object? it doesn't ever seem to be called, instead you just send raw data with res.send(NikitaBellucci) Commented Dec 5, 2019 at 21:42

1 Answer 1

1

Your function is attached to the entire result object, but you get the 0 property of it in (await auth.login(req, db, crypto))[0]; which won't have the function. Just remove the [0] and NikitaBellucci.isAdministrator will be the function in question.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.