使用BeforeSave更新解析对象

时间:2016-03-02 17:35:49

标签: javascript android android-studio parse-platform cloud-code

我制作了一个简单的货币交易应用,user1使用parse.com和云代码将资金转移到user2,使用masterkey授予user1权限,以覆盖所有其他权限

final ParseQuery<ParseObject> parseQuery = ParseQuery.getQuery(user.getClassName());
            Log.d("class found : ",String.valueOf(user.getClassName()));
            parseQuery.whereMatches("AccountNumber", mAccountNumber.getText().toString().trim());
            parseQuery.getFirstInBackground(new GetCallback<ParseObject>() {
                @Override
                public void done(final ParseObject parseObject, ParseException e) {
                    if (parseObject != null) {
                        Log.d("userID",String.valueOf(parseObject.getObjectId()));
                        balance = Integer.parseInt(mbalance.getText().toString());
                        q = parseObject.getInt("balance");
                        parseObject.put("balance", balance + q);
                        parseObject.saveInBackground(new SaveCallback() {
                            @Override
                            public void done(ParseException e) {
                                if (e == null) {
                                    Toast.makeText(TransferToAccount.this, "DOne!!", Toast.LENGTH_SHORT).show();
                                } else {
                                    Log.d("Exception", "1");
                                    e.printStackTrace();
                                }
                            }
                        });
                    } else {
                        Log.d("Exception", "2");
                        Toast.makeText(TransferToAccount.this, "No user found ", Toast.LENGTH_SHORT).show();
                        e.printStackTrace();

这是可以代码:

Parse.Cloud.beforeSave(Parse.User, function(request, response) {
Parse.Cloud.useMasterKey(); 
var user = request.user;
if (user.existed()) { return; }
user.setACL(new Parse.ACL(user));
user.save();
// add user to role
var roleName = "member";
var roleQuery = new Parse.Query(Parse.Role);
roleQuery.equalTo("name", roleName);
roleQuery.first().then(function(role) {
role.getUsers().add(user);
// save role
return role.save();
});

当我尝试转移时,我得到Exception

W/System.err: com.parse.ParseException: java.lang.IllegalArgumentException: Cannot save a ParseUser that is not authenticated.

W/System.err: Caused by: java.lang.IllegalArgumentException: Cannot save a ParseUser that is not authenticated.

抱歉,如果我不清楚。

2 个答案:

答案 0 :(得分:0)

在保存之前,您无法为用户分配角色。尝试将其更改为afterSave触发器。

答案 1 :(得分:0)

阅读this之后,我可以通过创建第二个类来解决我的问题,该类带有指向mc1.x = data["mydata"]["mc1"].x; //etc .... 的指针而无需云代码。