Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.92k stars 3.84k forks source link

model.find({}) returning the value of " [ ] " #5950

Closed TravJav closed 6 years ago

TravJav commented 6 years ago

`var mongoose = require('mongoose'), Schema = mongoose.Schema, bcrypt = require('bcrypt'), SALT_WORK_FACTOR = 10;

var FinduserSchema =  new Schema({
    email: { type: String, required: true, index: { unique: true } },
    username: { type: String},
    password: { type: String, required: true }
});

module.exports = mongoose.model('Finduser', FinduserSchema,'Finduser');

====================================================== var Finduser = require('../MongoSchemas/FindUser');

// POST Request In Context Of Login function perform_login(P_email, P_password) { var Email = JSON.stringify(P_email); var Password = JSON.stringify(P_password); //console.log(" \n \n VALUES: " + Email + Password); mongoose.connect('mongodb://localhost/healthDB'); mongoose.connection.on('error', function (err) { assert.equal(null, err); console.log("Connected successfully to server"); });

Finduser.find({ email: "helloCanada@gmail.com" }, function (err, users) {
    if (err)
        res.send(err);
    else
        console.log(JSON.stringify(users));  // returns  [] always!

});

}

` I am trying to implement this code for login purposes the function above receives email and password information as args, can confirm this is not the issue. I have been stuck on the implementation of the find(); I have read the docs and am quite confused seeing how this should work. I can see with my Mongo DB compass that the appropriate docs are present but still every time I run this method I am receiving " [ ] " Can someone point me in the right direction ? in the code above module.exports there is a 3rd arg present because of the tendency of pluralization with mongo models and in the function above " users" is a collection where all the login information such as email, password, username exist. Thanks in advance !

TravJav commented 6 years ago

Got this one a few hours later was just a question of how to do this properly and with that method.. heres the logic

` var search_results = Finduser.findOne({ 'email': Email}, function (err, user) { if (user) console.log(user); else res.send(err);

});

`

This code will retrieve the user. I was a bit worried at first I would have to use a different auth method since this was slowing me down but hopefully what I have written helps a other person understand when faced with the issue.