Expressjs基于参数检索集合

时间:2015-02-05 02:40:10

标签: node.js express routes mongoose

我正在尝试根据视图中某个架构属性的参数请求来循环我的数据库集合。我的架构中有一个tags属性,保存为数组。我希望能够做的是单击保存到该数组的其中一个值,这将呈现包含所选标记值的集合的页面。我已经能够创建路径,它将引导我查看单击的单个标记,但是当我在视图中调用循环时,会收到tags: [xxx, xxx, xxx] has no method .forEach。为什么会这样,我该如何解决?

错误讯息:

TypeError: /Users/user/Desktop/Projects/node/blog/views/pages/tag.ejs:15
   13| <div class="col-md-12">
   14| <h1><%= blogpost.tags %></h1>
>> 15| <% blogpost.forEach(function(blogpost) { %>
   16| <%= blogpost.title %>
   17| <% }); %>
   18| </div>

Object { _id: 54c7bd20c58f389232000001,
 category: 'Analytics/SEO/SEM',
 content: '',
 tagline: 'yep',
 author: 'Author',
 blogUrl: 'roger',
 featureImage: '/images/event-placeholder.png',
 title: 'Roger',
 __v: 0,
 date: Tue Jan 27 2015 11:29:48 GMT-0500 (EST),
 tags: [ 'wolf', ' cow', ' monkey' ] } has no method 'forEach'

这是我的模特:

var mongoose    = require('mongoose');
var mongoosePaginate = require('mongoose-paginate');
var Schema      = mongoose.Schema;



var BlogPostSchema  = new Schema({
        title: { type: String, unique: true },
        featureImage: String,
        blogUrl: String,
        author: String,
        tagline: String,
        category: String,
        content: String,
        tags: { type: Array, lowercase: true },
        date: { type: Date, default: Date.now() }
});

BlogPostSchema.post('init', function (post) {
    var date = new Date(post.date || Date.now() );
    post.dateString = date.getMonth() + 1 + '/' + date.getDate() + '/' + date.getFullYear();

});

BlogPostSchema.plugin( mongoosePaginate );

var Blogpost = mongoose.model("Blogpost", BlogPostSchema);



module.exports = mongoose.model('Blogpost', BlogPostSchema);

以下是我在路线中调用单独选择的标签的方式(我的函数是否使用正确的参数调用方法?):

router.route('/admin/posts/create')

    // START POST method
        .post(function(req, res) {

            console.log("New instance");
            console.log(req.body.tags);

            var blogpost = new Blogpost(); // create a new instance of a Blogpost model

            blogpost.title = req.body.title; // set the blog title
            blogpost.featureImage = req.body.featureImage; // set the blog image
            blogpost.blogUrl = blogpost.title.toLowerCase().replace(/\s+/g,"-");
            blogpost.author = req.body.author; // set the author name
            blogpost.tagline = req.body.tagline; // set the tagline
            blogpost.content = req.body.content; // set the blog content
            blogpost.category = req.body.category; // set the category
            blogpost.tags = req.body.tags.trim().split(","); // set the tags
                //Save Blog Post
                blogpost.save(function(err) {
                    if (err)
                        res.send(err);

                    res.redirect(303, '/'); //NEEDS TO BE CHANGED
                });

        }) // END POST method


        .get(isLoggedIn, function(req, res, blogpost) {
            res.render('pages/blogpost-create', {
              blogpost : blogpost
            });
        });

function getTagCriteria(params){
  return {
    tags: params.blogpost_tags
  };
}

router.route('/tag/:blogpost_tags')

    .get(function (req, res) {
      var tagCriteria = getTagCriteria(req.params);
      Blogpost.findOne(tagCriteria, function (err, blogpost) {
        if (err)
          res.sent(err);
        res.render('pages/tag', {
            blogpost : blogpost
        })
      })
    });

页面/标签视图文件:

<div class="container">
        <div class="col-md-12">
            <h1><%= blogpost.tags %></h1>
            <% blogpost.forEach(function(blogpost) { %>
                <%= blogpost.title %>
            <% }); %>
        </div>
    </div>

1 个答案:

答案 0 :(得分:0)

将您的mongo查询更改为:

  Blogpost.find(tagCriteria, function (err, blogpost) {
    if (err)
      res.sent(err);
    res.render('pages/tag', {
        blogpost : blogpost
    })
  })

注意:blogpost现在将是一个数组[]而不是一个对象{}

然后在您的玉石中,执行以下操作

<% blogpost.forEach(function(post) { %>
   <%= post.title %>
<% }); %>