niahmiah / mongoose-uuid

uuid type for mongoose
21 stars 24 forks source link

Problem with find methods with mongoose 5.4.19 #19

Closed ystreibel closed 5 years ago

ystreibel commented 5 years ago

Hi everyone, I'm using Mongo 4.0 and those libraries in a NodeJS backend:

"mongoose": "^5.3.0",
"mongoose-auto-increment": "^5.0.1",
"mongoose-paginate": "^5.0.3",
"mongoose-uuid2": "^2.3.0",

With the implemented schemas:

'use strict';
var uuidv4 = require('uuid/v4');
var mongoose = require('mongoose');
var mongoosePaginate = require('mongoose-paginate');
var mongooseUuid = require('mongoose-uuid2')(mongoose);
var Schema = mongoose.Schema;
var bcrypt = require('bcrypt');

var UserSchema = new Schema({
                                _id: { type: mongoose.Types.UUID, default: uuidv4 },
                                username: {type: String, unique: true},
                                datacollect_id: {type: String, unique: true, required: true},
                                password: {type: String},
                                age: {type: Number},
                                gender: {type: String, enum: ["F", "M"]},
                                handedness: {type: String, enum: ["left", "right"]},
                                language: { type:  mongoose.Types.UUID, ref: 'Language'},
                                isTest: {type: Boolean, required: true, default: false},
                                swipeExperience: {type: String, enum: ["never", "first_experience", "regular_user", "expert"]},
                                keyboardExperience: {type: String, enum: ["never", "first_experience", "regular_user", "expert"]},
                                creationDate: {type: Date, default: Date.now, required: true},
                                creationBy: { type:  mongoose.Types.UUID, ref: 'User', required: true },
                                expirationDate: {type: Date},
                                updateDate: {type: Date, required: true},
                                enabled: {type: Boolean, required: true},
                                roles: {
                                    type: [String], enum: ["USER"], required: true
                                },
                                collects: [{
                                    collect: { type:  mongoose.Types.UUID, ref: 'Collect', required: true},
                                    sessions: {type: [{
                                        stepsIndex: {type: Number, required: true},
                                        inks: {type: [{
                                            _id: {type:  mongoose.Types.UUID, ref: 'Ink'}
                                        }]},
                                        inksGoal: {type: Number, required: true},
                                        beginDate: {type: Date, required: true},
                                        endDate: {type: Date},
                                        updateDate: {type: Date, required: true}
                                    }]}
                                }]
                            },
                            {
                                id: false
                            });

UserSchema.plugin(mongoosePaginate);

UserSchema.pre('save', function (next) {
    var user = this;
    if (this.password && (this.isModified('password') || this.isNew)) {
        bcrypt.genSalt(10, function (err, salt) {
            if (err) {
                return next(err);
            }
            bcrypt.hash(user.password, salt, function (err, hash) {
                if (err) {
                    return next(err);
                }
                user.password = hash;
                next();
            });
        });
    } else {
        return next();
    }
});

UserSchema.methods.comparePassword = function (passw, cb) {
    bcrypt.compare(passw, this.password, function (err, isMatch) {
        if (err) {
            return cb(err);
        }
        cb(null, isMatch);
    });
};

mongoose.set('debug', true);

module.exports = mongoose.model('User', UserSchema);

But when my backend trying to do this:

 User.findOne({ username: username }, function(err, user) {...});

the result user is empty. I tried others "find" methods and same result.

this is the mongoose log:

Mongoose: users.findOne({ username: 'admin' }, { projection: {} })

My database is full and nothing happened in MongoDB logs.

ystreibel commented 5 years ago

I made a mistake in the name of my collection for mongoose connection.