admin管理员组文章数量:1421054
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();}
});
});
本文标签: javascriptCannot read property 39password39 of nullStack Overflow
版权声明:本文标题:javascript - Cannot read property 'password' of null - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745318961a2653286.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论