Gie-Sakura
Gie-Sakura

Reputation: 117

Cannot read property 'password' of null

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

Upvotes: 2

Views: 6984

Answers (3)

Gie-Sakura
Gie-Sakura

Reputation: 117

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 compara = respuesta.password;
  if (compara=="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();}
  });
});

Upvotes: 1

chridam
chridam

Reputation: 103365

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

Upvotes: 1

mildog8
mildog8

Reputation: 2154

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();
    }
  });
  ...

Upvotes: 1

Related Questions