Node js 错误。有专家吗?
2015-12-08
108
我是 nodejs 的新手。我遇到了这个错误,现在我无法启动我的 npm。请告诉我如何重新启动 npm 以及如何避免遇到此问题
错误如下:
{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' }
js-bson: Failed to load c++ bson extension, using pure JS version
C:\Users\sriramr\nodetest1\routes\index.js:129
collection.find({},{}function(e,docs){
^^^^^^^^
SyntaxError: Unexpected token function
at exports.runInThisContext (vm.js:73:16)
at Module._compile (module.js:443:25)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (C:\Users\sriramr\nodetest1\app.js:11:14)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
npm ERR! Windows_NT 6.1.7601
npm ERR! argv "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files\\nodejs
\\node_modules\\npm\\bin\\npm-cli.js" "start"
npm ERR! node v0.12.0
npm ERR! npm v2.5.1
npm ERR! code ELIFECYCLE
npm ERR! [email protected] start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script 'node ./bin/www'.
npm ERR! This is most likely a problem with the nodetest1 package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node ./bin/www
npm ERR! You can get their info via:
npm ERR! npm owner ls nodetest1
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! C:\Users\sriramr\nodetest1\data\npm-debug.log
源代码::INDEX.JS
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
res.render('index', { title: '欢迎来到会议室演示。' }); });
router.get('/userlist', function(req, res) {
var db = req.db;
var collection = db.get('usercollection');
collection.find({},{},function(e,docs){
res.render('userlist', {
"userlist" : docs
});
});
});
router.get('/newuser',function(req,res){
res.render('newuser',{title:'Add new user'});
});
router.get('/newuser',function(req,res){
res.render('newroom',{title:'Add new room'});
});
router.post('/adduser', function(req, res) {
var db = req.db;
var userName = req.body.username;
var userEmail = req.body.useremail;
var collection = db.get('usercollection');
collection.insert({
"username" : userName,
"email" : userEmail
}, function (err, doc) {
if (err) {
res.send("There was a problem adding the information to the database.");
}
else {
res.redirect("userlist");
}
});
});
/*---------adding room number and room list -------*/
router.post('/addroom', function(req, res) {
var db = req.db;
var roomname = req.body.roomname;
var collection = db.get('roomlist');
collection.insert({
"roomname" : roomname
}, function (err, doc) {
if (err) {
res.send("There was a problem adding the information to the database.");
}
else {
res.redirect("roomlist");
}
});
});
/*----------room end list ------------------*/
/*--------addingtime list ----------*/
router.post('/addtime', function(req, res) {
var db = req.db;
var starttime = req.body.strt_time;
var endtime = req.body.end_time;
var emailid = req.body.empemail;
var collection = db.get('timelist');
collection.insert({
"starttime" : starttime,
"endtime" : endtime,
"emailid": emailid
}, function (err, doc) {
if (err) {
res.send("There was a problem adding the information to the database.");
}
else {
res.redirect("addtime");
}
});
var collection = db.get('roomlist');
collection.find({},{},function(e,docs){
res.render('roomlist', {
"roomname" : roomname
});
});
});
/*---------removing time list --------*/
/*----removing room number and roomlist -----------*/
router.post('/removeroom', function(req, res) {
var db = req.db;
var roomname = req.body.remove_room;
var collection = db.get('roomlist');
collection.remove({
"roomname" : roomname
}, function (err, doc) {
if (err) {
res.send("There was a problem adding the information to the database.");
}
else {
res.redirect("roomlist");
}
});
});
router.get('/roomlist', function(req, res) {
var db = req.db;
var collection = db.get('roomlist');
collection.find({},{}function(e,docs){
res.render('roomname', {
"roomname" : docs
});
});
});
/*----------end room number and room list ---------*/
/*--end db connection for user ---*/
module.exports = router;
1个回答
我不认为这是 Node.js 的问题,只是 Javascript 的问题。
据报道,似乎破坏了这一切的代码是:
collection.find({},{}function(e,docs){
^^^^^^^^
基本上,您不能在“{}”之后立即提供函数。如果这是列表中“collection”对象的“find”方法调用的参数,则应在前面加上逗号,就像:
collection.find({},{},function(e,docs){
修复版本(index.js 文件底部):
router.get('/roomlist', function(req, res) {
var db = req.db;
var collection = db.get('roomlist');
collection.find({},{},function(e,docs){ // your error fixed!!!
res.render('roomname', {
"roomname" : docs
});
});
});
/*----------end room number and room list ---------*/
/*--end db connection for user ---*/
module.exports = router;
Dmitri Sologoubenko
2015-12-08