2014-11-04 55 views
0

上我有2個集合如何獲得集合名稱express.js服務器

appoinments 
invoice 

在我的崗位方法,保存數據之前,我檢查這些數據已經存在,但我只想要appoinments集合。 我想正常保存invoice收集。我怎樣才能做到這一點。我已經試過如果陳述,但它沒有奏效。

app.post('/collections/:collectionName', function(req, res, next) { 
    console.log(req.body[0]) 
    if('/collections/:collectionName' == '/collections/appoinments'){ 
     req.collection.findOne({hour_responce:req.body[0].hour_responce}, function(e, result){ 
      if(result){ 
      console.log(result); console.log(e) 
      res.send(500,{error:"You Already have a Task on this Time Period"}) 
      } 
      else{ 
      req.collection.insert(req.body, {}, function(e, results){ 
      if (e) return next(e) 
      res.send(results) 

      }) 
      } 
     }) 
    } 
    else{ 
    req.collection.insert(req.body, {}, function(e, results){ 
      if (e) return next(e) 
      res.send(results) 
     }) 
    } 

}) 
+1

'如果( '/收藏/:集合名' == '/收藏/ appoinments')'永遠比賽。嘗試'if(req.params.collectionName =='appointmentments')' – 2014-11-04 12:12:22

+0

@ ben-fortune謝謝+1 – kosala 2014-11-05 07:51:03

回答

1

我不能添加評論,所以我嘗試通過添加一個答案

首先給出一些建議:

約會或appoinments?小心拼寫

第二張:

本·福特是正確的。可能是使用:

如果(req.params.collectionName == 'appoinments')

三:

看來你正在使用MongoDB的(我看到一些關鍵字,比如收集,findOne)。使用本地mongdb節點驅動程序查詢集合的正確方法如下: db.collection.findOne(query,callback);

老實說,我不明白 「req.collection.findOne」

好運

+0

是的,你是對的我的錯誤它的appoinmnets:D – kosala 2014-11-05 07:50:43

相關問題