Ad

Check The Result Of FindOne Mongodb Nodejs

mongo.connect(url, {useNewUrlParser: true}, function(error, db) {
    if (error) throw error;
    var dbo = db.db("mydb");
    console.log("connected to databse successfully");
    dbo.collection("users").findOne({}, {"email": email}) (function(err, result) {
        if (!result) {
            res.sendfile(__dirname + '/register.html');
        } else {
            dbo.collection("users").insertOne(data, function(err, collection){
                if (err) throw err;
                console.log("Record inserted successfully");
                console.log(collection);
                res.status(200).send('You are loged in, Welcome!');
            });
        }
    });
});

I want to check the result. If the mail is already in the db I don't add it in the record. But it doesn't work

Ad

Answer

Your findOne query is incorrect.

dbo.collection("users").findOne({}, {"email": email}) (function(err, result){

Should Be

dbo.collection("users").findOne({"email": email}) (function(err, result) {
Ad
source: stackoverflow.com
Ad