Skip to content Skip to sidebar Skip to footer

Unable To Retrieve Data From Api Using Express Nodejs And Mongodb, Loading

I'm attempting to create a Rest API using Node.js, Express, and MongoDB. I am currently running on my local host :3000. When I try to restart and run the server I am using the rout

Solution 1:

I think Ashley is on the right track. But to make it more clear where the problem is happening try using this as a guide: http://expressjs.com/en/guide/routing.html

app.get('/drinks', function (req, res) {
  drink.findAll(req, res);
});

Then you can add logging in between this call and in your findAll function.

Solution 2:

Your models (drinks.js) accept two parameters (req & res) but on your route you don't pass in any parameters.

Try the following:

app.get('/drinks', function(req, res) {
    drink.findAll(req, res);
});

app.get('/drinks/:id', function(req, res){
    drink.findById(req, res);
});

Alternatively, you could achieve the same with a callback based structure:

server.js

...

app.get('/drinks', function(req, res) {
    drink.findAll(function(err, drinks){
        res.send(drinks)
    });
});

...

drinks.js

...

exports.findAll = function(callback) {
    db.collection('drinks', function(err, collection) {
            collection.find().toArray(function(err, drinks) {
                    callback(err, drinks)
                });
        });
};

(error handling required) ...

Post a Comment for "Unable To Retrieve Data From Api Using Express Nodejs And Mongodb, Loading"