parse.com - Error: 101 object not found for update -
i've got follwing parse.com cloud code
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 parse.error error code , 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 parse.error error code , description. console.log("error: " + error.code + " " + error.message); } }); }, error: function (object, error) { // object not retrieved successfully. // error parse.error error code , description. console.log("error: " + error.code + " " + error.message); } });
option1 try save queried book after setting bookstatus returned book.
option2 try save new book object after setting book.id
book.id = bookid; book.set("bookstatus", new bookstatus({id: newbookstatus}));
however of 2 options end getting in parse.com logs:
error: 101 object not found update error: 101 object not found update
any idea of doing wrong?
thanks in advance!
--edit
new implementation 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 parse.error error code , description. console.log("error: " + error.code + " " + error.message); } }); }, error: function (object, error) { // object not retrieved successfully. // error parse.error error code , description. console.log("error: " + error.code + " " + error.message); } });
});
with result:
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 update
your aftersave hook overwriting fetched book's object id. if you're setting same object id on it, object thinks it's dirty , it's no longer valid reference.
avoid using book returned get()
, not updating it's 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 parse.error error code , description. console.log("error: " + error.code + " " + error.message); } }); }, error: function (object, error) { // object not retrieved successfully. // error parse.error error code , description. console.log("error: " + error.code + " " + error.message); } }); });
since know id book, instead of using query, can create pointer , fetch directly.
final aftersave hook:
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 parse.error error code , description. console.log("error: " + error.code + " " + error.message); } }); }, error: function (object, error) { // object not retrieved successfully. // error parse.error error code , description. console.log("error: " + error.code + " " + error.message); } }); });
Comments
Post a Comment