小编Ror*_*120的帖子

Model.find().toArray()声称没有.toArray()方法

我是nodejs和mongodb的新手,我正在尝试拼凑我自己的博客应用程序.我在尝试通过我的"博客"模型查询具有特定用户名的模型时遇到问题.当我试图跑

var userBlogs = function(username) {
  ub = Blog.find({author: username}).toArray();
  ub = ub.reverse();
};
Run Code Online (Sandbox Code Playgroud)

我收到一个错误.

TypeError: Object #<Query> has no method 'toArray'
Run Code Online (Sandbox Code Playgroud)

我知道全局变形很糟糕,但我一直试图让它起作用.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);
Run Code Online (Sandbox Code Playgroud)

这是/ 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('/'); …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb node.js passport.js pug

10
推荐指数
1
解决办法
2万
查看次数

标签 统计

mongodb ×1

mongoose ×1

node.js ×1

passport.js ×1

pug ×1