2013-06-25 45 views
0

我传递一个JSON变量的模块,但我不能做我的收藏的更新,我总是有更新的错误。为什么更新不工作

var gestion = function(myJSON) { 
var dburl = 'localhost/mongoapp'; 
var collection = ['clientes']; 
var db = require('mongojs').connect(dburl, collection); 

     function cliente(nombre, estado, nuevo){ 
       this.nombre = nombre; 
       this.estado = estado; 
       this.nuevo = nuevo; 
     } 

     var cliente1 = new cliente(myJSON.nombre myJSON.estado, myJSON.nuevo); 

if (cliente1.estado == "desconectado"){ 
       db.clientes.update(cliente1.nombre, {$set: {estado: "desconectado", nuevo: "no"}}, function(err) { 
         if (err) console.log("error "+cliente1.nombre); 
         else console.log("OK"); 
         }); 
} 

}   

return 0; 
} 

我还试图删除我的数据库,并创建一个更多的时间,我敢肯定,我的目标在我的数据库存在。

+0

和什么是您收到的错误消息? – AntonioOtero

+0

错误:选择器必须是有效的JavaScript对象 – user2369478

回答

2

,你应该使用的签名是

update(query, update, callback) 

,但你通过为query一个字符串,这并不意味着什么蒙戈。你可能想看看the docs的概述,但对于这个特定的情况下,它看起来像你想查找文档,其中​​等于在cliente1.nombre的字符串。对此的查询是一个字典{ nombre: cliente1.nombre },所以行应该是

db.clientes.update({nombre: cliente1.nombre}, {$set: {estado: "desconectado", nuevo: "no"}}, function(err) {