2

我有以下 parse.com 云代码

Parse.Cloud.afterSave("Action", function (request) {

var BookStatus = Parse.Object.extend("BookStatus");
var Book = Parse.Object.extend("Book");
var book = new Book();

var actionType = request.object.get("actionTypePointer").id;
var bookId = request.object.get("bookPointer").id;

var queryBook = new Parse.Query("Book");
var newBookStatus;

queryBook.get(bookId,{
    success: function (gottenBook) {

        newBookStatus = "idOfTheBookStatus"; 

        book.id = bookId;
        book.set("bookStatus", new BookStatus({id: newBookStatus}));
        gottenBook.set("bookStatus", new BookStatus({id: newBookStatus}));

        //OPTION 1
        gottenBook.save(null,{
            success: function(data) {
                console.log("Bookstatus updated1");
            },
            error: function (data,error) {
                // error is a Parse.Error with an error code and description.
                console.log("Error: " + error.code + " " + error.message);
            }
        });
        //OPTION 2
        book.save(null,{
            success: function(data) {
                console.log("Bookstatus updated2");
            },
            error: function (data,error) {
                // error is a Parse.Error with an error code and description.
                console.log("Error: " + error.code + " " + error.message);
            }
        });

    },
    error: function (object, error) {
        // The object was not retrieved successfully.
        // error is a Parse.Error with an error code and description.
        console.log("Error: " + error.code + " " + error.message);
    }

});

选项1 我尝试在将 bookStatus 设置为返回的书后保存查询的书。

选项2 我尝试在设置 book.Id 后保存新书对象

        book.id = bookId;
        book.set("bookStatus", new BookStatus({id: newBookStatus}));

但是,使用 2 个选项中的任何一个,我最终都会进入 parse.com 日志:

错误:101 未找到更新对象错误:101 未找到更新对象

知道我做错了什么吗?

提前致谢!

- 编辑

使用 fetch 的新实现:

Parse.Cloud.afterSave("Action", function (request) {

var BookStatus = Parse.Object.extend("BookStatus");
var Book = Parse.Object.extend("Book");
var book = new Book();

var actionType = request.object.get("actionTypePointer").id;
var bookId = request.object.get("bookPointer").id;

book.id = bookId;
console.log("before fetch book.id" + book.id);
var newBookStatus;

book.fetch({
    success: function (book) {
        newBookStatus = "XMFkXS9NVv";            

        book.set("bookStatus", new BookStatus({id: newBookStatus}));

        console.log("book" + book);
        console.log("book.id" + book.id);
        console.log("book.isValid()" + book.isValid());

        book.save(null,{
            success: function(data) {
                console.log("Book Status updated to:" +newBookStatus);
            },
            error: function (data,error) {
                // error is a Parse.Error with an error code and description.
                console.log("Error: " + error.code + " " + error.message);
            }
        });
    },
    error: function (object, error) {
        // The object was not retrieved successfully.
        // error is a Parse.Error with an error code and description.
        console.log("Error: " + error.code + " " + error.message);
    }

});

});

结果:

  Input: {"place":{"__type":"GeoPoint","latitude":41.354643806134625,"longitude":2.121594674804572},"bookLocationDescription":"sad","bookPointer":{"__type":"Pointer","className":"Book","objectId":"kWcALge4az"},"actionTypePointer":{"__type":"Pointer","className":"ActionType","objectId":"kJC954w9iO"},"userPointer":{"__type":"Pointer","className":"_User","objectId":"6xpiAHX9Ju"},"createdAt":"2013-05-16T13:59:33.810Z","updatedAt":"2013-05-16T13:59:33.810Z","objectId":"PwlXhKL51l","ACL":{"6xpiAHX9Ju":{"read":true,"write":true},"*":{"read":true}}}
  Result: Success

I2013-05-15T20:52:19.170Z] before fetch book.idc1iKxw3NLD
I2013-05-15T20:52:19.273Z] book[object Object]
I2013-05-15T20:52:19.273Z] book.idc1iKxw3NLD
I2013-05-15T20:52:19.273Z] book.isValid()true
I2013-05-15T20:52:19.325Z] Error: 101 object not found for update
4

3 回答 3

6

您的 afterSave 钩子正在覆盖获取的 Book 的对象 ID。即使您在其上设置相同的对象 ID,该对象现在仍认为它是脏的并且不再是有效的引用。

通过使用返回的书get()而不更新它的 id 来避免这种情况:

    Parse.Cloud.afterSave("Action", function (request) {

        var BookStatus = Parse.Object.extend("BookStatus");
        var Book = Parse.Object.extend("Book");
        var book = new Book();

        var actionType = request.object.get("actionTypePointer").id;
        var bookId = request.object.get("bookPointer").id;

        var queryBook = new Parse.Query("Book");

        var newBookStatus;

        queryBook.get(bookId,{
            success: function (book) {
                newBookStatus = "idOfTheBookStatus"; 

                book.set("bookStatus", new BookStatus({id: newBookStatus}));

                book.save(null,{
                    success: function(data) {
                        console.log("Bookstatus updated1");
                    },
                    error: function (data,error) {
                        // error is a Parse.Error with an error code and description.
                        console.log("Error: " + error.code + " " + error.message);
                    }
                });
            },
            error: function (object, error) {
                // The object was not retrieved successfully.
                // error is a Parse.Error with an error code and description.
                console.log("Error: " + error.code + " " + error.message);
            }

        });
    });

由于您已经知道这本书的 id,因此您可以创建一个指针并直接获取它,而不是使用查询。

最终的 afterSave 钩子:

    Parse.Cloud.afterSave("Action", function (request) {

        var BookStatus = Parse.Object.extend("BookStatus");
        var Book = Parse.Object.extend("Book");
        var book = new Book();

        var actionType = request.object.get("actionTypePointer").id;
        var bookId = request.object.get("bookPointer").id;

        book.id = bookId;

        var newBookStatus;

        book.fetch({
            success: function (book) {
                newBookStatus = "idOfTheBookStatus"; 

                book.set("bookStatus", new BookStatus({id: newBookStatus}));

                book.save(null,{
                    success: function(data) {
                        console.log("Bookstatus updated1");
                    },
                    error: function (data,error) {
                        // error is a Parse.Error with an error code and description.
                        console.log("Error: " + error.code + " " + error.message);
                    }
                });
            },
            error: function (object, error) {
                // The object was not retrieved successfully.
                // error is a Parse.Error with an error code and description.
                console.log("Error: " + error.code + " " + error.message);
            }

        });
    });
于 2013-05-15T18:17:50.767 回答
2

这是我在跑来跑去之后的做法

ParseCrashReporting.enable(this);

// Enable Local Datastore.
Parse.enableLocalDatastore(this);

// Add your initialization code here
Parse.initialize(this, "*******", "******");

ParseUser.enableAutomaticUser();
ParseACL defaultACL = new ParseACL();

ParseACL.setDefaultACL(defaultACL, true);

// save the installation
ParseInstallation.getCurrentInstallation().saveInBackground();

有关此问题的更多信息,请参阅我的博客文章:http: //aprogrammersday.blogspot.com/2015/02/fix-parse-101-error-object-not-found.html

于 2015-02-10T10:02:26.453 回答
0

在寻找这个错误时,我发现这个线程最有用。我想补充一点,object not found for get本质上也可以表示foreign key reference not found.

not found for get错误可能非常无益。对我来说,它甚至没有给出发生错误的行号,错误消息非常模糊,我在任何文档中都找不到对该短语的引用。

如果您收到此错误,您会发现很多引用权限问题,但如果您已经在使用,master key那么您需要在所选对象上找到被外键引用的对象。

于 2015-07-29T08:24:01.063 回答