Parse server edit (put) a specific object with Node.js

281 views Asked by At

So I am trying to setup an Node app with Parse and now I am trying to edit (put) a Object. The example on the Parse page wasn't really helpful. So I tried something alone. The Result:

 app.put('/edit/user', function (req, res) {
  var user = new Parse.Query('person');
  user.equalTo('objectId', req.body.id);
  user.find().then(function (result) {
    if (req.body.username != null) {
      result.set('username', req.body.username);
    }
    if (req.body.lastname != null) {
      result.set('lastname', req.body.lastname);
    }
    if (req.body.firstname != null) {
      result.set('firstname', req.body.firstname);
    }
    result.save(null).then(function (edit) {
      res.send(edit);
    })
  })
})

Now what happen is: It fiend the object and result is right too. But when some attribute (like lastname) isn't null it goes in the "if" statement and then hang up by the "result.set(" ")". It also send nothing back. So in postman the calls never gets back.

Is there a diffrent way I can do it or is there some syntax error in my code.

Thank you already!

1

There are 1 answers

4
Shekhar Tyagi On

You can do it simply just like that if some value is there then it goes in if condition otherwise it goes out. And some other conditions as well

   app.put('/edit/user', function (req, res) {
      var user = new Parse.Query('person');
      user.equalTo('objectId', req.body.id);
      user.find().then(function (result) {
       if(req){
        if (req.body.username) {
          result.set('username', req.body.username);
        }
        if (req.body.lastname) {
          result.set('lastname', req.body.lastname);
        }
        if (req.body.firstname) {
          result.set('firstname', req.body.firstname);
        }
        result.save(null).then(function (edit) {
          res.send(edit);
        })
       }else{
       console.log('undefined');
       }
      })
    })