我正在使用Express中的REST Web服务,我希望得到一个包含我在参数中放置的小时数的对象。
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
router.get('/test', function(req, res) {
var responseObject = { messages: 'OK' };
res.send(responseObject);
})
router.get('/api/consommation/:heure', function(req, res) {
var heure = req.query.heure;
var responseObject = [{
"heure": "08",
"prix": "0.1593"
}, {
"heure": "09",
"prix": "0.1593"
}, {
"heure": "01",
"prix": "0.1252"
}, {
"heure": "02",
"prix": "0.1252"
}, {
"heure": "03",
"prix": "0.1252"
}];
var jsonContent = JSON.parse(responseObject);
jsonContent.forEach(function(obj) {
obj.forEach(function(o) {
if (o['heure'] == heure)
console.log(o);
});
});
res.json(jsonContent);
});
module.exports = router;
所以我想返回包含参数及其价格中输入小时数的对象。
答案 0 :(得分:1)
错误就在那里因为你试图在JS对象上使用JSON.parse
而不是字符串(该方法用于解析JSON字符串)。
只需放弃var jsonContent = JSON.parse(responseObject);
行并直接使用responseObject
即可。
答案 1 :(得分:1)
直接使用responseObject
并移除内部foreach循环。
heure = 01;
var responseObject = [
{
"heure": "08",
"prix": "0.1593"
},
{
"heure": "09",
"prix": "0.1593"
},
{
"heure": "01",
"prix": "0.1252"
},
{
"heure": "02",
"prix": "0.1252"
},
{
"heure": "03",
"prix": "0.1252"
}
];
responseObject.forEach(function (obj) {
if (obj['heure'] == heure)
console.log(obj);
})

答案 2 :(得分:0)
不需要var jsonContent = JSON.parse(responseObject);
因为json已经全部解析了。
替换
var jsonContent = JSON.parse(responseObject);
通过
var jsonContent =responseObject;
答案 3 :(得分:0)
这是最终守则:
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
router.get('/test', function(req, res){
var responseObject = {messages : 'OK'};
res.send(responseObject);
})
router.get('/api/consommation', function(req, res, next){
var heure = req.param('heure');
var responseObject = [
{ "heure" :"08",
"prix" : "0.1593" },
{ "heure" :"09",
"prix" : "0.1593" },
{ "heure" :"10",
"prix" : "0.1593" },
{ "heure" :"11",
"prix" : "0.1593" },
{ "heure" :"12",
"prix" : "0.1593" },
{ "heure" :"13",
"prix" : "0.1593" },
{ "heure" :"14",
"prix" : "0.1593" },
{ "heure" :"15",
"prix" : "0.1593" },
{ "heure" :"16",
"prix" : "0.1593" },
{ "heure" :"17",
"prix" : "0.1593" },
{ "heure" :"18",
"prix" : "0.1593"},
{ "heure" :"19",
"prix" : "0.1593" },
{ "heure" :"20",
"prix" : "0.1593" },
{ "heure" :"21",
"prix" : "0.1593" },
{ "heure" :"22",
"prix" : "0.1593" },
{ "heure" :"23",
"prix" : "0.1593" },
{ "heure" :"00",
"prix" : "0.1252" },
{ "heure" :"01",
"prix" : "0.1252"},
{ "heure" :"02",
"prix" : "0.1252" },
{ "heure" :"03",
"prix" : "0.1252" },
{ "heure" :"04",
"prix" : "0.1252" },
{ "heure" :"05",
"prix" : "0.1252" },
{ "heure" :"06",
"prix" : "0.1252" },
{ "heure" :"07",
"prix" : "0.1252" },
];
responseObject.forEach(function (obj) {
if (obj['heure'] == heure)
res.json(obj);
else
return next;
});
});
module.exports = router;

我的主要问题是我没有把下一个'我的函数中的参数,稍后再返回 谢谢你们所有人