2014-01-20 83 views
0

請幫我解決這個問題。我是node以及json
一些事情一定會變得非常糟糕。json發送查看,但無法從中獲取id玉模板

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}); 
    } 
+0

請格式化您的問題。玉是空白敏感的。如果您無法找出降價來設置問題的格式,那麼空白敏感的模板語言可能不是您的最佳選擇。 –

+0

對不起,是的,我已格式化它。 – jyoti

回答

0

在您的JSON(實際上Javascript對象),trips不是數組。而是trips.onwardTrips是一個數組。 由以下請更改each在玉像

if trips.onwardTrips 
    each trip in trips.onwardTrips 
    p #{trip.tripId} 
+0

我收到錯誤。 TypeError:/home/jyoti/catchthatbus/views/trips.jade:6 4 | h3旅程選擇
#{trips} 5 | form.form-horizo​​ntal(id =「Findtrips」,accept-charset =「UTF-8」,action =「#」,method =「post」enctype =「multipart/form-data」) > 6 |每趟trip.onwardTrips 7 | p#{trip.fromCity} 8 | 9 | 無法讀取未定義的屬性'長度' – jyoti

+0

確保'旅行'具有'onwardTrips'數組。從錯誤中,onwardTrips是未定義的。我已經更新了答案,以檢查對象中是否存在'onwardTrips'。 –

0

解決它。我得到了trip.tripId現在我想要獲取內部數組,即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}); 

    }