我尝试从mongodb获取数据并将其作为json格式发送到html页面
我的代码是:
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);
然后它会出现以下错误:
/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
答案 0 :(得分:3)
如下所述更改您的第48行。 db.collection.find(
不会返回您的数据。一旦db.collection.find({})
函数执行,它将进入回调函数 db.collection.find({},callback
)
db.collection('urlinfo').find({}, function(err, products){
if(err){
console.log(err);
res.json(err);
}
else{
res.json(products);
}
});