node js + express + jade iterate array

时间:2016-03-01 22:51:54

标签: javascript node.js express

我尝试做的是以玉石布局迭代数组。布局文件是lessons.jade:

   each lesson in myLessons
            ul.nav.pull-center: li.dropdown.nav.text-center
              .btn.btn-default.dropdown-toggle.btn-lg.btn-block(data-toggle="dropdown" aria-expanded="false")= lesson.day
              ul.dropdown-menu.col-xs-12
                each lessonName in myLessons
                  li: a(href='/lessons/details')= lessonName.name
                  li.divider

我的视图控制器文件是lessons.js:

var renderLessonPage = function (req, res, responseBody) {
    var message;
    if (!(responseBody)) {
    message = "Lessons API Error!";
  } else {
    if (responseBody.length < 0) {
      message = "No lessons found!";
    }
  }
  res.render('lessons', {
    title: 'Lesson page',
    pageHeader: {
      title: 'Just a page'
    },
    myLessons: responseBody,
    message: message
  });
};



module.exports.lessons = function(req, res) {
  var requestOptions, path;
  path = '/api/locations/' + req.params.locationid + '/lessons/';
  requestOptions = {
    url: apiOptions.server + path,
    method: "GET",
    json: {}
  };
  request(
      requestOptions,
      function (err, response, body) {
        renderLessonPage(req, res , body );
      }
  );

};

我的API控制器文件包含:

var sendJsonResponse = function (res, status, content) {
  res.status(status);
  res.json(content);
};

module.exports.lessons = function (req, res) {
  loc
      .findById(req.params.locationid)
      .populate('lessons')
      .exec(function (err, location) {
        if (!location) {
          sendJsonResponse(res, 404, {
            "message": "No lessons found!"
          });
        } else {
          response = {
            location: {
              lessons: location.lessons
             // id: req.params.locationid
            }
          };
          sendJsonResponse(res, 200, response);
        }
      })
};

如果我启动Chrome并浏览到API访问的位置,我会看到以下json结果:

{
  "location": {
    "lessons": [
      {
        "_id": "56d5d947bdb5c3d92ace848c",
        "name": "Henk",
        "startTime": "13:00",
        "endTime": "14:00",
        "day": "Tuesday",
        "__v": 0
      },
      {
        "_id": "56d5d9dfea5cbcf42a20f87e",
        "name": "skaaak",
        "startTime": "12:00",
        "endTime": "18:00",
        "day": "Monday",
        "__v": 0
      }
    ]
  }
}

如果我通过console.log在我的玉石布局模板中启用- console.log并抓住myLessons中的课程,则在控制台中输出完全相同的内容。但我不能在我的玉器布局中使用数组中的值。

我只有一个下拉菜单而不是两个,文本没有填充在下拉菜单中,下拉菜单中有两个空项目。

我尝试了很多东西,但大多数都是undefined或无法阅读的属性。

1 个答案:

答案 0 :(得分:0)

当您为请求发送结果时

# in a migration
execute "create index on posts using gin(to_tsvector('english', body));"

# console query
> my_post = Post.create! title: 'my post', body: 'this post is about physics and ...'
> Post.basic_search('physics').first #=> <my_post>    

响应是对象,而不是数组。所以当 renderLessonPage 函数渲染时(选项 myLessons:responseBody )是对象。

您可以按以下方式替换代码:

response = {
    location: {
      lessons: location.lessons
     // id: req.params.locationid
    }
};
  sendJsonResponse(res, 200, response);