2012-06-01 55 views
0

我嘗試使用貓鼬和expressjs查找和文章。我有搜索表單,輸入設置爲變量'titless'。發現/搜索貓鼬和expressjs

這裏是我的app.js文件:

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 
     } 
     }); 
    }); 

}); 

和繼承人findbytitle方法:

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) 
     }); 
     } 
    }); 

}; 

當我點擊搜索按鈕,它向前我到localhost /? titless =關鍵字頁面,但我仍然看到索引頁面沒有搜索 文章:/。

回答

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

我不是100%確定,但是您是否必須定義當地人?它不應該是以下嗎?

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

可能是。但我不能檢查,因爲我得到錯誤,雖然我有要求('mongoose'):500 TypeError:對象[對象對象]沒有方法'找到' – Spy

+0

您顯示的代碼沒有方法查找。 – Pickels

+0

我嘗試使用以下方法:http://pastebin.com/eiqBMuwZ – Spy