如何在back4app中更新用户对象? [英] How to update the user object in back4app?

查看:154
本文介绍了如何在back4app中更新用户对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Node.js和back4app.com

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:

默认情况下,Parse.User类是安全的,除非使用经过验证的方法(如logInsignUpcurrent save方法. >

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

代码中的外观如何?

好吧,我明白了.当我弄清楚它的时候,我发现了一些小的演出塞子.我将其列出给可能关注的任何人.

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.

感谢@RamosCharles我在Parse._initialize中添加了主密钥.只有使用该.save(null,{useMasterKey:true})才有效.请注意,没有null的情况也将不起作用.

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.

那是我的工作代码:

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

现在我想知道

  • 为什么我的工作代码不同于文档?

    我的代码有多安全?怎样使它更安全?

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

    推荐答案

    是的,他们的API参考非常有用!在此部分中,有一个尝试JSFiddle"按钮,您已经看到了吗?

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

    要更新用户对象,必须使用主密钥.在前端,不建议这样做,最好创建一个云代码函数并在您的前端调用它.但是,出于测试目的,您可以继续使用API​​参考,但是在JSFiddle上,您需要进行一些更改,这是它们的示例代码,但需要进行以下调整:

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

    您需要像在query.find上一样,在"Parse._initialize"中的"initialize"之前插入"_",并在您的查询中插入主键.

    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.

    这篇关于如何在back4app中更新用户对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆