Thursday 20 April 2017

Mongoose is connected but can't find data

I have a problem and could really use some help. We had an application that used MySQL and we are switching over to MongoDB. We are using Node.js with Express and have MongoDB and Mongoose for the database

We have looked at the MongoDB and Mongoose documentation and searched for related questions on Stack Overflow but we seem to be missing something.

This is what we have in app.js:

var mongo = require('mongodb');
var mongoose = require('mongoose');
var db = mongoose.connect('mongodb://localhost:27017/DBName');

This is our usersModel.js

var db          = require('mongoose');
var Schema      = db.Schema;

var userSchema  = new Schema({
    u_id :                      Number,
    u_name :                    { type: String, required: true },
    u_lastname :                String
});

module.exports  = db.model('user', userSchema);

And this is what we use in the userController:

var User        = require('../models/usersModel');

User.find({}, function(err, users) {
    if (err) throw err;

    console.log("Found users: ", users);
});

The console.log(db.connection.readyState); says it is connecting. And the User.find() doesn't seem to give out an error, but instead gives an empty array/undefined.

We would really appreciate it if someone could tell us what we overlook.

Thanks in advance.



via Mark_at_SwingBy

No comments:

Post a Comment