0

getCurrentUser()当我使用方法将其标记为已验证时,Parse android SDK 不允许更新用户表中的列。当我调用saveInBackground()它时,我在日志文件中收到以下错误:

未捕获的内部服务器错误。{ [MongoError: exception: Mod on _id not allowed] name: 'MongoError'

下面是我用来保存的代码:

byte[] data = "Working at Parse is great!".getBytes();
        final ParseFile file = new ParseFile("abcdef.txt", data);



    ParseUser currentUser = ParseUser.getCurrentUser();
        if (currentUser != null) {
            // do stuff with the user
            currentUser.put("column_name", file);
            currentUser.saveInBackground(new SaveCallback() {
                @Override
                public void done(ParseException e) {
                    if (e == null) {
                        Log.i("KQ", "update successfully");
                    } else {
                        Log.i("KQ", "update error e = " + e);
                    }
                }
            });
        } else {
            // show the signup or login screen
            Log.i("KQ", "else");
        }
4

2 回答 2

0

直到我得到一个好的答案。我目前在解析中使用我的云代码 beforeSave 触发器来删除 id 字段以摆脱异常并解除阻止我的工作。任何好的答案仍然会受到赞赏。

我现在使用的代码在云代码中如下所示。

Parse.Cloud.beforeSave(Parse.User, function(request, response) {
    // For Android SDK remove id field while updating a user becasue it was creating MongoDB Exception in android.
    if (request.object.get("updatedFrom") == 'android') {
        request.object.unset("updatedFrom");
        request.object.unset("id");
    }
    response.success();
});

谢谢。

于 2017-10-02T05:30:45.493 回答
0

在将 ParseFile 附加到 User 对象之前,您必须保存它。你的代码应该是这样的

byte[] data = "Working at Parse is great!".getBytes();
    final ParseFile file = new ParseFile("abcdef.txt", data);
    file.saveInBackground(new SaveCallback() {
        @Override
        public void done(ParseException e) {
            if(e==null)
            {
                ParseUser currentUser = ParseUser.getCurrentUser();
                if (currentUser != null) {
                    // do stuff with the user
                    currentUser.put("column_name", file);
                    currentUser.saveInBackground(new SaveCallback() {
                        @Override
                        public void done(ParseException e) {
                            if (e == null) {
                                Log.i("KQ", "update successfully");
                            } else {
                                Log.i("KQ", "update error e = " + e);
                            }
                        }
                    });
                } else {
                    // show the signup or login screen
                    Log.i("KQ", "else");
                }
            }
        }
    });
于 2017-10-01T15:09:46.380 回答