2017-09-05 15 views
-1

我正在使用nodejs訪問mongodb.I能夠靜態查詢它通過直接指定字段值,但我想查詢它使用的值用戶將輸入它,我應該如何處理它。這是我的HTML文件,我接受用戶的位置和時間值,get方法在表單動作中調用指定的url:http://localhost:3000/movies,where我添加了find()查詢以從集合中檢索所有記錄,例如:如何在使用nodejs和mongoose的文本字段中使用用戶提供的輸入來查詢mongodb?

 var query={'location':"//the location which user will enter in textbox"}; 
    db.collection("movie").find(query); 

我的形式:

<html> 
    <body> 

     <form action = "http://localhost:3000/movies"> 
     First Name: <input type = "text" name = "location"> <br> 
     Last Name: <input type = "text" name = "timing"> 
     <input type = "submit" value = "Submit"> 
     </form> 

    </body> 
</html> 

幫助我怎樣才能達致這? 謝謝!

+1

您正在使用什麼服務器?除了前端和數據庫之外,您不指定任何內容。 – Paul

+0

您缺少關鍵部分。一個Fullstack應用程序通常基於一個前端(您發佈的HTML中的表單),一個後端,用於清理並檢查用戶在表單中輸入的內容,以及數據庫來保存數據。您在這裏省略後端細節。 – lilezek

+0

@lilezek使用mongodb數據庫,並且我正在使用nodejs進行兩者之間的通信! –

回答

-1

你可以試試這個:

db.yourCollection.findOne({ location: 'the location which user will enter in textbox'}, function (err, info) { 
    console.log(info) 
}); 
+0

這不是OP問題的解決方案。 – lilezek

+0

得到您的數據在 request.body.param, 你怎麼能投票給它, 它是由你問的標誌? –

+0

有什麼要求。什麼身體。什麼參數?天哪。 – lilezek

0

我決定繼續和嘗試,並回答你的問題,儘管有限的信息。

基本上,您的nodejs應用程序必須接收表單數據,然後將其放入查詢中。

如果您使用快遞,你會安裝體解析器,並從那裏:

// in your app setup code 
const bodyParser = require('body-parser'); 
// ... any other requires you have 

app.use(bodyParser.urlencoded()); 
// ... any other app.use statements you have 

app.post('/movies', (req, res) => { 
    var query = { location: req.body.location }); 
    // the remainder of your code for handling the query and sending a response 
}); 
相關問題