Question

I pass a json variable to a module but I can't do the update of my collection, always I have an error in the updating.

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

I also tried to remove my db and create one more time and I'm sure that my object exist in my db.

Was it helpful?

Solution

The signature you should be using is

update(query, update, callback)

but you're passing a string for query, which doesn't mean anything to Mongo. You may want to look at the docs for an overview, but for this specific instance, it looks like you're trying to find the document where nombre is equal to the string at cliente1.nombre. The query for this is a dictionary { nombre: cliente1.nombre }, so that line should be

db.clientes.update({nombre: cliente1.nombre}, {$set: {estado: "desconectado", nuevo: "no"}}, function(err) {
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top