最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot read property 'password' of null - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a ment  | 

3 Answers 3

Reset to default 1

The 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();}
  });
});
发布评论

评论列表(0)

  1. 暂无评论