[英]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.