2015-03-03 83 views
1

我正在嘗試設置在MongoDB中一個文檔中的字段,但我得到以下錯誤,當我運行node app.js設置文檔場 - MongoDB的

拋兒; //未處理 '錯誤' 事件

那裏有我的代碼:

app.get('/verify', function(req, res) { 
    console.log(req.protocol + ":/" + req.get('host')); 
    var collection = "usuarios"; 
    collectionDriver.getCollection(collection, function(error, the_collection) { 
     if (error) { 
      res.send(400, error); 
     } else 
      the_collection.update("{_id:" + req.query.id + "}, {$set: {ativo:'T'}}"); 
    }); 
}); 

編輯:

app.get('/verify',function(req,res){ 
console.log(req.protocol+":/"+req.get('host')); 
    var collection = "usuarios"; 
    collectionDriver.getCollection(collection, function(error, the_collection) { 
     if (error) { res.send(400, error);} 
     else 
      the_collection.update({ '_id': req.query.id, $set: { ativo: 'T' } }); 
     // the_collection.update("{_id:'"+req.query.id+"'}, {$set: {ativo:'T'}}");} 
    }); 


}); 

和誤差

events.js:72 throw er; // Unhandled 'error' event ^ Error: listen EADDRINUSE at errnoException (net.js:904:11) at Server._listen2 (net.js:1042:14) at listen (net.js:1064:10) at Server.listen (net.js:1138:5) at Object.<anonymous> (/home/ladessa/files/MelhoraCidade/server/app.js:206:24) at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10)

+0

您可以顯示完整的錯誤回溯? – thefourtheye 2015-03-03 03:29:49

+0

是的,請...看我的編輯 – Developer2012 2015-03-03 03:33:41

+0

看起來你啓動服務器的端口已經被另一個進程打開了。 – thefourtheye 2015-03-03 03:34:30

回答

2

作爲每mongoose's update documentation,所述update方法應該用mi來調用最少兩個參數。引用函數簽名,

Model.update(conditions, update, options, callback); 
  1. conditions - 有效的JavaScript對象找到實際的文件進行更新。

  2. update - 實際的更新對象,它決定了文檔中所做的實際更改。

注意在情況下,你使用本地的MongoDB驅動程序,update函數接受類似的參數。引述update文檔,

update(selector, document, options, callback) 

...

selector更新操作的選擇。

document更新文檔。

所以,你的功能被改變這樣

the_collection.update({ 
    '_id': req.query.id 
}, { 
    $set: { 
     ativo: 'T' 
    } 
});