简体   繁体   中英

find/search on mongoose and expressjs

I try to find and article using mongoose and expressjs. I have search form and the input is set to variable 'titless'.

Here is my app.js file:

app.get('/', function(req, res){
    postdb.findAll( function(error,docs){
        res.render('index.jade', {
            locals: {
                title: 'test',
                articles:docs
            }
        });
    })

});

app.post('/.:titles?', function(req, res) {
    postdb.findByTitle(req.params.titless, function(error, article) {
        res.render('blog_show.jade',
        { locals: {
            title: article.title,
            article:article
        }
        });
    });

});

and heres findbytitle method:

PostDB.prototype.findByTitle = function(titless, callback) {
    this.getCollection(function(error, article_collection) {
      if( error ) callback(error)
      else {
        article_collection.findOne({title: titless}, function(error,
result) {
          if( error ) callback(error)
          else callback(null, result)
        });
      }
    });

};

When i click search button it does forward me to localhost/? titless=keyword page, but i still see index page without searched articles :/.

res.render('blog_show.jade', { 
  locals: {
    title: article.title,
    article:article
  }
});

I am not 100% sure but do you have to define locals? Shouldn't it be the following?

res.render('blog_show.jade', { 
  title: article.title,
  article:article
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM