0

Hello I am getting no result on browser when running this code. Documents showing in my console but now showing in browser. May I know where is the mistake. I am newbie to nodejs and following the book but it is not working well. API.JS

    var http = require('http');
    var url = require('url');
    var database = require('./database');

    function findAllProducts(resourceName,req,res){
            database.find('Orderbase','order','{}',(err,documents)=>{
            res.writeHead(200,{'Content-Type':'application/json'});
            res.end(JSON.stringify(documents));
        });
    };

    var server = http.createServer((req,res)=>{
        parsedURL = url.parse(req.url,true);
        switch(parsedURL.pathname)
        {
            case '/api/order/':
                if (parsedURL.query.id) 
                {
                    findProductById(id,req,res);
                }
                else
                {
                    findAllProducts(req,res);
                }   
            break;
            default:
            res.end(JSON.stringify("you shell not pass!"));
        }

    });
    server.listen(8080);
    console.log('Up, running and ready for action!');

    database.js

    var MongoClient = require('mongodb').MongoClient;
    var assert = require('assert');
    var connect = function (databaseName, callback) {
    var url = 'mongodb://localhost:27017/' + databaseName;

MongoClient.connect(url, (error, database)=> {
        assert.equal(null, error);
        console.log("Successfully connected to MongoDB instance!");
        callback(database);
       });
};
    exports.find = (databaseName, collectionName, query)=> {
       connect(databaseName, (database)=> {
       var collection = database.collection(collectionName);
       collection.find(JSON.parse(query)).toArray(
          (err, documents)=> {
          assert.equal(err, null);
          console.log("MongoDB returned the following documents:");
          console.dir(documents);
          database.close();
        })
      })
    };
  • 1
    `res.end` is used for returning the response with no data. it should be `res.send`. – AZ_ Feb 07 '20 at 11:18
  • Does this answer your question? [What is the difference between res.end() and res.send()?](https://stackoverflow.com/questions/29555290/what-is-the-difference-between-res-end-and-res-send) – AZ_ Feb 07 '20 at 11:21
  • Sorry but I just change it from res.send to res.end. res.send is also not showing data on browser – Arslan Khan Feb 07 '20 at 11:47

2 Answers2

0

@Arslan can you try res.json({documents : documents}) instead of res.end(JSON.stringify(documents));

if it won't give answer properly then ;

var document = JSON.Stringify(documents); res.json({documents : document})

0

I found the soultion by debugging the code there was two problems one callback was missing in database.js

exports.find = (databaseName, collectionName, query,callBack)=> { //callback was missing
    connect(databaseName, (database)=> {
        var collection = database.collection(collectionName);
        collection.find(JSON.parse(query)).toArray((err, documents)=> {
           assert.equal(err, null);
           callBack(documents); // callback was missing
           database.close();
           return; // return was missing 
        })
    })
};
Mohammad Faisal
  • 2,144
  • 15
  • 26