I'm making a little database using mongodb and nodejs, I want to Update a field but I have this error, the code is, the name of the model is "ListaSalas":
router.post('/updatesala', function(peticion, responsep){
var password = peticion.body.password;
var url = peticion.body.url;
ListaSalas.findOne({'url': url}, function (err, respuesta) {
var PassBusca = respuesta.password;
if(PassBusca){
responsep.writeHead(200, {"Content-Type": "text/html"});
responsep.write("passwordmal");
responsep.end();
}else{
ListaSalas.update({url: url}, {password: password});
responsep.writeHead(200, {"Content-Type": "text/html"});
responsep.write("passwordok");
responsep.end();
}
});
Does anybody know where my mistake is please? Thank you
I'm making a little database using mongodb and nodejs, I want to Update a field but I have this error, the code is, the name of the model is "ListaSalas":
router.post('/updatesala', function(peticion, responsep){
var password = peticion.body.password;
var url = peticion.body.url;
ListaSalas.findOne({'url': url}, function (err, respuesta) {
var PassBusca = respuesta.password;
if(PassBusca){
responsep.writeHead(200, {"Content-Type": "text/html"});
responsep.write("passwordmal");
responsep.end();
}else{
ListaSalas.update({url: url}, {password: password});
responsep.writeHead(200, {"Content-Type": "text/html"});
responsep.write("passwordok");
responsep.end();
}
});
Does anybody know where my mistake is please? Thank you
Share Improve this question asked Dec 23, 2015 at 23:29 Gie-SakuraGie-Sakura 1172 silver badges11 bronze badges3 Answers
Reset to default 1The issue is on the result returned from the findOne()
method, if there is no match then respuesta
is null hence the error
Cannot read property 'password' of null
To get around this, use the updateOne()
method directly and in the callback check whether the document has been modified:
router.post('/updatesala', function(peticion, responsep){
var password = peticion.body.password;
var url = peticion.body.url;
ListaSalas.updateOne({'url': url}, {'password': password}, function (err, result) {
if (err) return err;
var PassBusca = result.result.n;
var pwd = PassBusca ? "passwordmal": "passwordok";
responsep.writeHead(200, {"Content-Type": "text/html"});
responsep.write(pwd);
responsep.end();
});
});
You are not checking if there is an error or not.
router.post('/updatesala', function(peticion, responsep){
var password = peticion.body.password;
var url = peticion.body.url;
ListaSalas.findOne({'url': url}, function (err, respuesta) {
if (err) return err;
var PassBusca = respuesta.password;
if(PassBusca){
responsep.writeHead(200, {"Content-Type": "text/html"});
responsep.write("passwordmal");
responsep.end();
}else{
ListaSalas.update({url: url}, {password: password});
responsep.writeHead(200, {"Content-Type": "text/html"});
responsep.write("passwordok");
responsep.end();
}
});
...
After checking the answers I could change the code, thanks to chridam's idea I used the method directly, instead of updateOne() it was update(), and in the database I put a default value in the password field ("password"), at the end the code is:
router.post('/updatesala', function(peticion, responsep){
var password = peticion.body.password;
var url = peticion.body.url;
ListaSalas.findOne({'url': url}, function (err, respuesta) {
var para = respuesta.password;
if (para=="password"){
ListaSalas.update({'url': url}, {'password': password},function (err, result) {
if (err) return err;
});
responsep.writeHead(200, {"Content-Type": "text/html"});
responsep.write('passwordok');
responsep.end();
}else{
responsep.writeHead(200, {"Content-Type": "text/html"});
responsep.write('passwordmal');
responsep.end();}
});
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745318962a4622352.html
评论列表(0条)