简体   繁体   中英

how to GET data from mongodb and transfer it to Html page using nodeJS?

I trying to fetch data from mongodb and send it to html page as json format

My code is :

server.js

var express = require("express"),
    app = express(),
    bodyParser = require('body-parser'),
    errorHandler = require('errorhandler'),
    methodOverride = require('method-override'),
    hostname = process.env.HOSTNAME || 'localhost',
    port = parseInt(process.env.PORT, 10) || 4004,
    publicDir = process.argv[2] || __dirname + '/public';
var exec = require('child_process').exec;
var fs = require('fs');
var MongoClient = require('mongodb').MongoClient
    , format = require('util').format;



//Show homepage
app.get("/", function (req, res) {
  res.redirect("/index.html");
  console.log("shubham ");
});
app.get("/index/", function (req, res) {
  res.redirect("/index.html");
  console.log("shubham ");
});

app.get("/search", function (req, res){
  console.log("shubham batra");
   var pro_name = req.query.name;
   var pro_code = req.query.code;
   var pro_category = req.query.category;
   var pro_brand = req.query.brand;
   var products;



  MongoClient.connect('mongodb://127.0.0.1:27017/prisync', function(err, db) {
  if (err) throw err;
    console.log("Connected to Database");


    var documen = {name:pro_name, code:pro_code , category:pro_category, brand:pro_brand };

  //insert record
  db.collection('urlinfo').insert(documen, function(err, records) {
    if (err) throw err;
    }

 var products = db.collection('urlinfo').find({});  //line 48 ======
 console.log(products);
 res.json(products);

  });
  db.close();
});
  console.log(res.body);

});
app.use(errorHandler({
  dumpExceptions: true,
  showStack: true
}));
//Search page
app.use(methodOverride());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: true
}));
app.use(express.static(publicDir));
app.use(errorHandler({
  dumpExceptions: true,
  showStack: true
}));

console.log("Server showing %s listening at http://%s:%s", publicDir, hostname, port);
app.listen(port);

then it gives following error :

 /home/shubham/Music/server/server.js:48 var products = db.collection('urlinfo').find({}); ^^^ SyntaxError: Unexpected token var at Module._compile (module.js:439:25) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:935:3 

Change your line 48 as mentioned below. db.collection.find( doesn't return you data. Once the db.collection.find({}) function is executed it will come to the callback function db.collection.find({}, callback )

db.collection('urlinfo').find({}, function(err, products){
    if(err){
        console.log(err);
        res.json(err);
    }
    else{
        res.json(products);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM