Model.find (). ToArray () claims to not have a .toArray () method

I am very new to nodejs and mongodb and I am trying to build my own blogging application. I am having a problem requesting in my Blog model for those with a specific username. When i try to run

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

I get an error message.

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

I know that globals are bad, but I was just trying to get it to work. The Mongo documentation states that the cursor is returned, which can call the toArray () method. I have no idea why this will not work. Here is my circuit / model creation:

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

Here are the / login and / readblog requests

 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('/'); }); 

The end result should work with this Jade:

 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 

How can I get a request to output an array or even work as an object?

+6
source share
3 answers

The toArray function exists in the Cursor class from the Native MongoDB NodeJS driver ( link ). The find method in MongooseJS returns a Query object ( referenc e). There are several ways to search and return results.

Since there are no synchronous calls in the NodeJS driver for MongoDB, in all cases you need to use an asynchronous template. MongoDB examples, which are often used in JavaScript using the MongoDB console, imply that the native driver also supports similar functions, which is not the case.

 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 no reason // to assign to something else ... blogs.reverse(); callback(err, blogs); }); }; 

Then to call it:

 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('/'); }); 

You can also sort by field (for example, via blog post):

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

The above code was sorted in descending order based on a field named postDate (alternative syntax: sort({ postDate: -1}) .

+3
source

You should use the find callback:

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

Now you can force blogs to use this feature:

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

Try something line by line:

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

All Articles