2015-06-21 38 views
0

我使用node.js与mongodb服务器进行通信。变量的发送和函数的输出正在工作。在数据库中写作是没有的,我找不到错误。这似乎是与插入一个问题,但我不能找到它.....在mongodb中写入失败/ node.js

mongo.js

​​

index.js

/* POST home page. */ 
router.post('/create_movement', function(req, res) { 
    var tblname = req.body.tablename, 
     OutUser = req.body.out_user, 
     OutEmail = req.body.out_email, 
     OutDate = req.body.out_date; 
     mongo.InsertDocument(tblname, OutUser, OutEmail, OutDate); 
     res.send("succesfull"); 
}); 

节点.js文件控制台

Example app listening at http://:::3005 
Connection established to mongodb://localhost:27017/straff 
function InsertDocument called 
test234 
POST /create_movement 500 317.042 ms - 954 

出其余客户端的

<!DOCTYPE html><html><head><title></title><link rel="stylesheet" href="/stylesheets/style.css"></head><body><h1>db is not defined</h1><h2></h2><pre>ReferenceError: db is not defined 
    at Object.InsertDocument (D:\test1\mongo.js:26:2) 
    at D:\test1\routes\index.js:17:9 
    at Layer.handle [as handle_request] (D:\test1\node_modules\express\lib\router\layer.js:82:5) 
    at next (D:\test1\node_modules\express\lib\router\route.js:110:13) 
    at Route.dispatch (D:\test1\node_modules\express\lib\router\route.js:91:3) 
    at Layer.handle [as handle_request] (D:\test1\node_modules\express\lib\router\layer.js:82:5) 
    at D:\test1\node_modules\express\lib\router\index.js:267:22 
    at Function.proto.process_params (D:\test1\node_modules\express\lib\router\index.js:321:12) 
    at next (D:\test1\node_modules\express\lib\router\index.js:261:10) 
    at Function.proto.handle (D:\test1\node_modules\express\lib\router\index.js:166:3)</pre></body></html> 
+0

我想你必须在你的** mongo.js **文件中实例化数据库。 – theamateurdataanalyst

+0

是的,就像theamateurdataanalyst建议的一样......在这个范围内似乎没有定义“db”变量。也许在你的mongo.js的顶部你需要这样的东西: var db = something; –

+0

我切换到猫鼬,现在就开始工作。谢谢大家 – Piet

回答

0

的这个错误告诉你的一切:

ReferenceError: db is not defined 
at Object.InsertDocument (D:\test1\mongo.js:26:2) 

你必须连接到数据库,并添加所有的细节到一个名为DB的变量。

使用Mongojs会是这样:

var mongojs = require('mongojs'); 
var db = mongojs(connectionString, [collections]); 

你必须检查文档为您使用的驱动程序。

+0

我切换到猫鼬,现在就开始工作。谢谢你们 – Piet