0

I'm creating a node.js REST API and i cannot query data in my API> here's the link to my code on github. Github line 150

And alo this is the code example

  api.get('/story', function (req, res) {
    Story.find({owner: req.decoded._id}, function (err, stories) {
        if (err) {
            res.send(err);
            return
        }
        res.json(stories);
    });
});

I want to find all the Stories creatd by logged user. o i send the user id in request. there are many posts done by user, but i'm getting a null array.

Chanaka De Silva
  • 406
  • 1
  • 7
  • 21
  • Possible duplicate of http://stackoverflow.com/questions/14183611/mongoose-always-returning-an-empty-array-nodejs. Can you check that first? – JohnnyHK Oct 23 '15 at 12:31
  • I think I have a different error here. Please check my code and then you will understand that. In line 99 , I have written a same method and it return me data. But other method in line 150 , its always getting empty array. Here's the link. https://github.com/chanakaDe/Mint-REST/blob/master/app/routes/api.js – Chanaka De Silva Oct 23 '15 at 13:02

2 Answers2

0

Use Mongoose debugging

var mongoose = require('mongoose').set('debug', true)

then look at your database request at nodejs console and debug it into mongodb console

check datatypes (string/ObjectId), check if it sends null as "owner"

vmkcom
  • 1,630
  • 11
  • 17
0

You have written the same GET api api.get('/story',function(){}); twice with different logics. This is the cause for the output. Because the api written in line 99 will get called always when there is /story api call.

Ketha Kavya
  • 558
  • 6
  • 18