2015-06-21 71 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