How to update the user object in back4app?

谁说我不能喝 提交于 2019-12-31 07:15:11

问题


I use Node.js and back4app.com

I try to update the user object. Therefore I have read a lot and found this promissing documentation:

let progressId = "xyz";
let userId = "12354"; //aka objectId
const User = new Parse.User();
const query = new Parse.Query(User);


// Finds the user by its ID
query.get(userId).then((user) => {
    // Updates the data we want
    user.set('progressId', progressId);
   // Saves the user with the updated data
   user.save()
       .then((response) => {
           console.log('Updated user', response);
       })
       .catch((error) => {
           console.error('Error while updating user', error);
       });
   });

But there also is a warning. It states:

The Parse.User class is secured by default, you are not able to invoke save method unless the Parse.User was obtained using an authenticated method, like logIn, signUp or current

How would this look like in code?

My solution

Well, I got it to work. While I figured it out, I have found some small show stoppers. I list it for anyone it may concern.

Thanks @RamosCharles I added the Master Key in Parse._initialize. Only with that .save(null, {useMasterKey: true}) works. Take notice, without null it also won't work.

That's my working code:

let progressId = "xyz";
const User = Parse.Object.extend('User'); //instead of const User = new Parse.User();
const query = new Parse.Query(User);

query.equalTo("objectId", '123xyz');
query.get(userId).then((userObj) => {
    // Updates the data we want
    userObj.set('progressId', progressId);

    // Saves the user with the updated data
    userObj.save(null, {useMasterKey: true}).then((response) => {
        console.log('Updated user', response);
    }).catch((error) => {
        console.error('Error while updating user', error);
    });
});

Now I'm wondering

  • why my working code is different from documentation?

  • how secure is my code? And what is to do to get it more secure?


回答1:


Yes, their API Reference is very helpful! On this section, there's a "try on JSFiddle" button, have you already seen that?

To update a user object, you must use the Master Key. On the frontend, it's not recommended, and it's better to create a cloud code function and call it on your frontend. However, for test purposes, you can keep using the API Reference, but on JSFiddle, you need to do some changes, here is their sample code, but with the adjustments:

Parse.serverURL = 'https://parseapi.back4app.com';
Parse._initialize('<your-appID-here>', '<your-JSKey-here>', '<Your-MasterKey-here>');

const MyCustomClass = Parse.Object.extend('User');
const query = new Parse.Query(MyCustomClass);

query.equalTo("objectId", "<object-ID-here>");
query.find({useMasterKey: true}).then((results) => {

  if (typeof document !== 'undefined') document.write(`ParseObjects found: ${JSON.stringify(results)}`);
  console.log('ParseObjects found:', results);
}, (error) => {
  if (typeof document !== 'undefined') document.write(`Error while fetching ParseObjects: ${JSON.stringify(error)}`);

  console.error('Error while fetching ParseObjects', error);
});

You'll need to insert the "_" before the "initialize" in your "Parse._initialize" and insert the Master Key in your query as I did on the query.find.



来源:https://stackoverflow.com/questions/58189064/how-to-update-the-user-object-in-back4app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!