json已发送到视图,但无法从其获取id
请帮我解决这个问题。我对node和json都是新手。 一定是出了什么大问题json已发送到视图,但无法从其获取id,json,node.js,pug,Json,Node.js,Pug,请帮我解决这个问题。我对node和json都是新手。 一定是出了什么大问题 extends layout block content h3 Trips selection #{trips} //shows following json in the view form.form-horizontal(id="Findtrips", accept-charset="UTF-8", action="#", method="post" enctype="multipart/form-
extends layout
block content
h3 Trips selection #{trips} //shows following json in the view
form.form-horizontal(id="Findtrips", accept-charset="UTF-8",
action="#", method="post" enctype="multipart/form-data")
each trip in trips
p #{trip.tripId} //doesnt pusblish anything
{trips}给出了以下内容:
{
"onwardTrips": [{
"tripId": "1285170",
"fromCity": "Singapore",
"toCity": "Malacca",
"operatorCode": "SA",
"operatorName": "Starmart Express",
"departTime": "2014-01-20 20:00:00.0",
"busType": "Executive",
"pickupPointDetails": [{
"pickupPointId": "78",
"departureTime": "2014-01-20 20:00:00.0",
"pickupPointName": "Golden Mile Tower, Beach Road"
}],
"dropoffPointDetails": [{
"dropOffPointName": "Melaka Sentral",
"dropOffPointId": "1285170"
}],
"fareDetails": {
"adultFare": "65.0"
}
},
{
"tripId": "1285254",
"fromCity": "Singapore",
"toCity": "Malacca",
"operatorCode": "SA",
"operatorName": "Starmart Express",
"departTime": "2014-01-20 23:00:00.0",
"busType": "Executive",
"pickupPointDetails": [{
"pickupPointId": "78",
"departureTime": "2014-01-20 23:00:00.0",
"pickupPointName": "Golden Mile Tower, Beach Road"
}],
"dropoffPointDetails": [{
"dropOffPointName": "Melaka Sentral",
"dropOffPointId": "1285254"
}],
"fareDetails": {
"adultFare": "65.0"
}
}],
"errorCode": 0
}
server.js
getTrips: function getTrips(req, res, next){
var url = 'CTB-WS/rest/trips?from='+ req.tripinfo.fromCityId + '&to=' + req.tripinfo.toCityId + '&depart-date=' + req.tripinfo.departDate+ '&pax=1';
console.log(url);
rest.get(url).on('complete', function(trips) {
if (trips instanceof Error) {
console.log('Error:', trips.message);
} else {
console.log('trips'+ JSON.stringify(trips));
req.trips = JSON.stringify(trips);
next();
}
});
},
sendTrips: function sendTrips(req, res, next){
res.render('trips', {trips : req.trips});
}
在jsonactually Javascript对象中,trips不是数组。而是trips.forwardTrips是一个数组。 请把每件衣服换成翡翠色
if trips.onwardTrips
each trip in trips.onwardTrips
p #{trip.tripId}
通过以下方式解决它。我得到trips.tripId现在我想得到trip内部的数组,即dropoffPointDetails
getTrips: function getTrips(req, res, next){
var url = 'CTB-WS/rest/trips?from='+ req.tripinfo.fromCityId + '&to=' + req.tripinfo.toCityId + '&depart-date=' + req.tripinfo.departDate+ '&pax=1';
console.log(url);
rest.get(url).on('complete', function(trips) {
if (trips instanceof Error) {
console.log('Error:', trips.message);
} else {
console.log('trips'+ JSON.stringify(trips));
trips = trips || [];
req.trips = trips['onwardTrips'];
next();
}
});
},
sendTrips: function sendTrips(req, res, next){
//res.render('trips', {trips : req.trips});
res.render('trips', { trips: req.trips});
}
请正确地编排你的问题。Jade对空格敏感。如果您无法找出标记来设置问题的格式,可能对空格敏感的模板语言不是您的最佳选择。对不起,是的,我已设置了格式。我遇到了错误。TypeError:/home/jyoti/catchthatbus/views/trips.jade:6 4 | h3 trips selection{trips}5 | form.form horizontalid=Findtrips,accept charset=UTF-8,action=,method=post enctype=multipart/form data>6 | trips.forwardTrips 7{trip.fromCity}8 | 9 |无法读取未定义的属性“length”确保行程具有ForwardTrips数组。从错误中可以看出,ForwardTrips是未定义的。我已经更新了答案,以检查对象中是否存在错误。