【问题标题】:Model.find().toArray() claiming to not have .toArray() methodModel.find().toArray() 声称没有 .toArray() 方法
【发布时间】:2014-01-18 10:42:13
【问题描述】:

我对 Node.js 和 MongoDB 非常陌生,正在尝试拼凑我自己的博客应用程序。我在尝试通过我的“博客”模型查询具有特定用户名的模型时遇到问题。当我尝试运行时:

var userBlogs = function(username) {
  ub = Blog.find({author: username}).toArray();
  ub = ub.reverse();
};

我收到一个错误:

TypeError: Object #<Query> has no method 'toArray'

我知道全局变量很糟糕,但我一直在努力让它发挥作用。 Mongo 文档声称返回的游标可以调用 toArray() 方法。我不知道为什么它不起作用。

这是我的架构/模型创建:

var blogSchema = mongoose.Schema({
  title: {type:String, required: true},
  author: String,
  content: {type:String, required: true},
  timestamp: String
});
var Blog = mongoose.model('Blog', blogSchema);

这里是 /login 和 /readblog 请求

app.get('/readblog', ensureAuthenticated, function(req, res) {
  res.render('readblog', {user: req.user, blogs: ub})
})

app.get('/login', function(req, res){
  res.render('login', { user: req.user, message: req.session.messages });
});

app.post('/login', 
  passport.authenticate('local', { failureRedirect: '/login'}),
  function(req, res) {
    userBlogs(req.user.username);
    res.redirect('/');
  });
});

最终的结果应该是和这个翡翠一起工作的:

extends layout

block content
    if blogs
        for blog in blogs
            h2= blog[title]
            h4= blog[author]
            p= blog[content]
            h4= blog[timestamp]
    a(href="/writeblog") Write a new blog

如何让查询输出数组,甚至作为对象工作?

【问题讨论】:

  • 应该将“猫鼬”添加到您的标签中

标签: node.js mongodb mongoose pug passport.js


【解决方案1】:

toArray 函数存在于本机 MongoDB NodeJS 驱动程序 (reference) 的 Cursor 类中。 MongooseJS 中的find 方法返回一个Query 对象(reference)。有几种方法可以进行搜索并返回结果。

由于 MongoDB 的 NodeJS 驱动程序中没有同步调用,因此在所有情况下都需要使用异步模式。 MongoDB 的示例(通常在 JavaScript 中使用 MongoDB 控制台)意味着本机驱动程序也支持类似的功能,但它不支持。

var userBlogs = function(username, callback) {
    Blog.find().where("author", username).
          exec(function(err, blogs) {
             // docs contains an array of MongooseJS Documents
             // so you can return that...
             // reverse does an in-place modification, so there's no reason
             // to assign to something else ...
             blogs.reverse();
             callback(err, blogs);
          });
};

然后,调用它:

userBlogs(req.user.username, function(err, blogs) {
    if (err) { 
       /* panic! there was an error fetching the list of blogs */
       return;
    }
    // do something with the blogs here ...
    res.redirect('/');
});

您还可以对字段进行排序(例如博客文章日期):

Blog.find().where("author", username).
   sort("-postDate").exec(/* your callback function */);

上述代码将根据名为postDate 的字段按降序排序(替代语法:sort({ postDate: -1})

【讨论】:

  • 这种排序正是我所需要的。我假设“postDate”只是一个连接到博客的新 Date() 对象?很像我的“时间戳”,还是您建议我更改日期(?)输出?
  • 是的,我以为是Date
【解决方案2】:

尝试以下方式:

Blog.find({}).lean().exec(function (err, blogs) {
  // ... do something awesome... 
} 

【讨论】:

    【解决方案3】:

    你应该利用 find 的回调:

    var userBlogs = function(username, next) {
        Blog.find({author: username}, function(err, blogs) {
            if (err) {
                ...
            } else {
                next(blogs)
            }
        })
    }
    

    现在你可以让你的博客调用这个函数了:

    userBlogs(username, function(blogs) {
       ...
    })
    

    【讨论】:

    • @WiredPrairie 这有帮助吗?
    猜你喜欢
    • 2012-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-08
    • 2021-11-20
    • 2019-12-09
    相关资源
    最近更新 更多