Javascript 车把问题
我目前正在尝试运行GET操作,但我的代码似乎不起作用。我已经完成了简单的设置,但是当我单击我创建的按钮从我创建的简单.JSON文件请求数据时,我一直得到“检索到0个文档”。有人能帮忙吗?这里是我的代码附在截图。非常感谢您的帮助。多谢各位 车把-Javascript 车把问题,javascript,html,json,handlebars.js,Javascript,Html,Json,Handlebars.js,我目前正在尝试运行GET操作,但我的代码似乎不起作用。我已经完成了简单的设置,但是当我单击我创建的按钮从我创建的简单.JSON文件请求数据时,我一直得到“检索到0个文档”。有人能帮忙吗?这里是我的代码附在截图。非常感谢您的帮助。多谢各位 车把- <!DOCTYPE html> <html> <head> <title>Get Request</title> </head> <body> <h
<!DOCTYPE html>
<html>
<head>
<title>Get Request</title>
</head>
<body>
<h2>GET Request</h2S>
<div class="results">
{{#each data}}
<p> {{first_name}} {{last_name}} </p>
{{/each}}
</div>
</body>
</html>
JSON-
{
"id": 1,
"name": {
"first_name": "Robert",
"last_name": "Swanson"
},
"title": "Some Dude"
}
请将您的代码粘贴到问题上。@HarshanaSerasing这些代码都附加在我问题的链接中,但由于我是新来的,我猜这可能是错误的做法。我将编辑帖子并实际显示代码。请将您的代码粘贴到问题中。@HarshanaSerasing代码都附加在我问题的链接中,但由于我是新来的,我猜这可能是错误的做法。我将编辑文章并实际显示代码。
const express = require("express")
const app = express();
const mongo = require('mongodb').MongoClient;
const assert = require('assert');
const url = 'mongodb://localhost:27017/test';
var bodyParser = require('body-parser');
var exphbs = require('express-handlebars');
app.set('view engine', 'handlebars');
app.engine('handlebars', exphbs());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.get('/api/user', function(req, res) {
mongo.connect(url,{ useUnifiedTopology: true }, function(err, database) {
const db = database.db('test');
db.collection('users').find()
.toArray(function(err, result) {
if (err) {
throw err;
}
console.log(result.length + ' documents retrieved.');
//res.json(result);
res.render('user', { data:result, layout:false})
database.close();
});
});
});
app.get('*', function (request, response) {
response.sendFile('./public/hello.html',{root: __dirname});
});
const port = 8900;
app.listen(port);
console.log(`Listening to server: http://localhost:${port}`)
{
"id": 1,
"name": {
"first_name": "Robert",
"last_name": "Swanson"
},
"title": "Some Dude"
}