json发送到视图但不能从它的jade模板中取id

时间:2014-01-20 04:47:32

标签: 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-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});
  }

2 个答案:

答案 0 :(得分:0)

在你的json(实际上是Javascript对象)中,trips不是数组。相反,trips.onwardTrips是一个数组。 请更改中的each

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

答案 1 :(得分:0)

通过以下解决方案。我得到了trip.tripOd现在我想在行程中获得数组,即下降点详情。

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

  }