4

Im new to nodejs.i already have a db named aqi with collection name pln. Im trying to show all the records in collection on web page but mongoose always returns empty array. i have tested it with other dbs but i can get data from them but for pln mongoose always return empty array. i would really appreciate it if someone could help me out. this is my schema

var Pln = new Schema({
    latit      : Number,
    longit     : Number,
    timestmp   : String,
    co         : Number,
    smoke      : Number,
    O3         : Number,
    humidity   : Number,
    temperature: Number,
    co2        : Number,
    dust       : String
});
var plnModel = mongoose.model('pln', Pln);

This is my route.

app.get('/', function(req, res) {
    res.contentType('application/json'); 
    plnModel.find({}, function(err, pln) {
        if (pln != null) {
            console.log('Found the User:' + pln.latit);
            res.send(JSON.stringify(pln));
        }
    });
});
4
  • How do you know it returns an empty array? You're not logging it. pln in an array, so testing for if (pln != null) is useless as this will always be true. On the other hand, pln.latit will always be undefined because [].latit is undefined. Also, why are you stringifying the documents? It can't work, because you set contentType('application/json') and you're sending back a string. Just send back res.status(200).json(pln). Commented Mar 19, 2018 at 13:55
  • I did log it and in console it also returns []. But i have tested it with my test database i can see all the data. Commented Mar 19, 2018 at 14:00
  • what is your DB name? how many records does it have? Commented Mar 19, 2018 at 14:29
  • db name is aqi and collection name is pln with about 1610 records Commented Mar 19, 2018 at 14:31

1 Answer 1

14

If you define your model this way : mongoose.model('pln', Pln), then your collection must be named 'plns' (= your model name + s )

If that's not the case, then you have to tell Mongoose that you have a custom collection name (in your case, from your comments, 'pln' without a s) by using :

mongoose.model('pln', Pln, 'pln'), the third argument being your collection's actual name.

And have a look at this code :

app.get('/', async (req, res) => {
    
    try {
        const pln = await plnModel.find().lean().exec(); // "lean" to get only JSON data (not Mongoose objects), faster
        console.log('Found Users :', pln);
        res.status(200).json(pln);
    } catch(err) {
        res.status(500).json(err);
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much man... your'e a life saver.. worked perfectly .
@JeremyThille thanks for your answer and for getting straight to the point. I found 2 other answers to this question that weren't really clicking for me but now I understand what went wrong.
@pdsla112 Haha well thank you ^^

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.