我怎样才能显示输出(HTML)取决于它的ID在节点js使用sqlite

这是我在Node JS上的代码。 你能举一个例子,我将在HTML代码中添加什么。 谢谢

exports.get = function(req, res){ db.all("SELECT * FROM f11", function(err,rows){ rows.forEach(function (row) { console.log('row: ' + row.GIVENNAME); }); // console.log(rows); res.render('form11.ejs',{array:rows}); }); }; 

我看到你正在使用sqlite3 npm包的sqlite。

看完你的代码后,你可以试试这个来达到你想要的:

更改get route有一个这样的参数id在你的server.js

 app.get('/legone/survey/surveyform/form11/:id', form11.get); 

并且,写下你的get函数,像这样:

 exports.get = function(req,res){ var id = req.params.id; db.all("SELECT * FROM f11 WHERE id = ?",id, function(err,rows){ rows.forEach(function (row) { console.log('row: ' + row.GIVENNAME); }); res.render('form11.ejs',{array:rows}); }); }; 

而你的route就是这样的:

 http://localhost/legone/survey/surveyform/form11/23 

其中23是id 。 把任何你想要的地方。

如果你正在使用ajax调用,它将如下所示:

 $http.get('/legone/survey/surveyform/form11/23') .success(function(response){ //handle response }) .error(function(error){ //handle error }); 

你可以通过POSTMAN或类似的东西来testing路由。

有关更多信息,您可以阅读Sqlite3 Wiki API文档,或直接了解params和Callbacks。

而且,在查看你的代码之后,你需要大量的重组。 你应该在一个文件中定义所有的路由,然后在app.js中使用它。

像这样的东西:

 //define all the routes that start from '/legone/survey/surveyform' in one file //(let it be 'surveryRoutes.js') //and then include it in your app.js. var surveyRoutes = require(./routes/surveyRoutes); app.use('/legone/survey/surveyform',surveyRoutes); 

如果你想得到一个ID和查询那么:

首先,您需要在请求查询中传递一个ID。 像http:// localhost / api / getUser?id = 23

 exports.getUser = function(req, res){ var user_id = req.query.id; db.all("SELECT * FROM f11 WHERE id="+user_id, function(err,row){ console.log(row); // Since you'll only get one row res.render('form11_detail.ejs',{data:row}); }); }; 

如果你添加这个代码,那么只显示一个你可以使用“getUser”函​​数并显示所有的数据,你可以使用你的“get”函数。