简体   繁体   English

如何在不更改当前用户的情况下在 Parse 中创建新用户?

[英]How can I create a new user in Parse, without changing the current user?

I am currently making a friend request module.我目前正在制作一个朋友请求模块。 The user clicks "Approve" the program adds the friend to the _User class on Parse Server.用户单击“批准”,程序将朋友添加到 Parse Server 上的_User类。 I believed the code below would do it, and it does, but the problem is that is changes the current user to the new friend that has been added.我相信下面的代码可以做到,而且确实如此,但问题是将当前用户更改为已添加的新朋友。 So if the current user is "Bob", and Bob adds "Mike" the new current user is Mike.因此,如果当前用户是“Bob”,并且 Bob 添加了“Mike”,则新的当前用户是 Mike。

I've been experimenting with signUp() , signUpInBackground() , but neither seem to work.我一直在试验signUp()signUpInBackground() ,但似乎都不起作用。

ParseUser newFriend = new ParseUser();
newFriend.setUsername(friendRequestFrom); //friendRequestFrom is a String that carries a name
newFriend.setPassword("12345");
newFriend.signUpInBackground(new SignUpCallback() {
    @Override
    public void done(ParseException e) {
        if(e == null){
            Log.i("Parse Result","Succesful!");
            Log.i("Current User",ParseUser.getCurrentUser().getUsername());
        }
        else{
            Log.i("Parse Result","Failed " + e.toString());
        }
    }
});

In order to create a new Parse user in Parse using client side code, you have to use a cloud code function hosted in your app backend.为了使用客户端代码在 Parse 中创建一个新的 Parse 用户,您必须使用托管在您的应用后端中的云代码功能。

In fact, for security reasons, client librararies are not permitted to directly add users.事实上,出于安全原因,不允许客户端库直接添加用户。

In your server side, create a file (main.js) containing the following code:在您的服务器端,创建一个包含以下代码的文件 (main.js):

Parse.Cloud.define("createNewUser", function(request, response) {
var User = Parse.Object.extend("User");
var us = new User();

us.set("username", request.params.username);
us.set("name", request.params.name);
us.set("email", request.params.email);
us.set("password", request.params.password);
us.save(null, {
    useMasterKey: true,
    success: function(obj) {
        response.success("user added");
    },
    error:function(err){
       response.error(err);         
    }
});       
});

Then, you can test this function in your javascript client code as following:然后,您可以在您的 JavaScript 客户端代码中测试此功能,如下所示:

var params =  { username: "userEmailAddress@yahoo.fr",
          email: "userEmailAddress@yahoo.fr",
          name:"Here write the user name",
          password:"TheUserPasswordHere"};

Parse.Cloud.run("createNewUser", params).then(function(response){console.log("response: "+response);}).catch(function (err) {
     console.log("error: "+err);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建从Java类到GlassFish的新用户? - How I can create new user from Java class to GlassFish? 如何在Windows上使用其他用户帐户创建新进程? - How can I create a new process with another User Account on Windows? 如何在Liferay中获取当前用户? - How can I get the current user in Liferay? 当用户在聊天中收到另一个用户的新消息时,如何创建通知? - How can I create a notification when a user receives new message from another user in my chat? 将 Intellij 许可证用户帐户更改为新帐户后,如何恢复 maven 依赖问题? - How can I recover maven dependency issues after changing my Intellij license user account to a new account? 如何在没有 Spring 安全性的情况下获取当前用户的 ID? - How can I get the current user's ID without Spring Security? 如何在不使用谷歌地图和其他地图的情况下获取用户当前位置? - How can I get user current location without using google maps and other maps? 我如何创建新的ArrayList <ArrayList<Integer> &gt;在不更改旧版本的情况下从旧版本中删除? - How can I create new ArrayList<ArrayList<Integer>> from an old one without changing the old one? 如何使用Java在当前用户的主目录中创建文件? - how can I create a file in the current user's home directory using Java? 如何创建 firebase 用户帐户 - How can I create firebase user account
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM