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?