2017-06-16 81 views
0

我想注入到NoSql數據庫。如何使用節點js中的功能

我在網上搜索試圖理解爲了注入一個簡單的HTML格式的表單和一個服務器端的node.js。 所以通常當我需要從mLab數據庫中獲取數據時,我只是使用查詢mycollection.find({name:req.body.name}),但正如我所理解的(請糾正我,如果我錯了,我不是很確定)我需要使用where函數,因爲使用這個函數可以很容易地注入我的數據庫。

這是我做過什麼

exports.getDetails = function(req, res){ 
    var myuser = req.body.user; 
    login.findOne({ $where : {name: myuser} }, function(err, docs){ 
     console.log(docs); 
    }); 
} 

但功能,其中不工作..我做了什麼錯?

+0

你說的「試圖注入」的意思是:如果你想獲得一個用戶只需做到這一點?從你的代碼看來,你真正想要做的就是獲取特定用戶的詳細信息。 –

+0

後來我想用我的輸入標籤來爲這個函數注入一個查詢以獲得DB(稍後)。 我需要稍後用用戶名注入它可以說它是拉拉然後一個函數,所以它會像 lala',function(){res.json('我可以插入thins');}' 這是形成什麼我看不知道它將如何工作呢.. 這個功能不適用於我甚至得到一個用戶.. – user193239

回答

0

使用下面的代碼片段。這將工作。

exports.getDetails = function(req, res){ 
    var myuser = req.body.user; 
    login.find({ $where : {name: myuser}}).toArray(function(err, docs){ 
    console.log(docs); 
}); 
} 
+0

TypeError:login.find(...)。toArray是不是一個函數 我得到這個錯誤消息 – user193239

0

從官方蒙戈文檔$where

Use the $where operator to pass either a string containing a JavaScript expression or a full JavaScript function to the query system. The $where provides greater flexibility, but requires that the database processes the JavaScript expression or function for each document in the collection. Reference the document in the JavaScript expression or function using either this or obj .

在你的代碼的發送對象是錯誤的(你只能發送JavaScript表達式作爲一個字符串或函數)。根據你的例子,我認爲你不需要$。

login.findOne({name: 'enterNameHere'},function(err, user){ 
    console.log(user); 
}); 

你可以找到$例子,其中的文檔(在頁面底部)