Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to push JSON Object in Array field of parse server #3846

Closed
zack1991 opened this issue May 22, 2017 · 5 comments
Closed

How to push JSON Object in Array field of parse server #3846

zack1991 opened this issue May 22, 2017 · 5 comments

Comments

@zack1991
Copy link

Hi, i am trying to push or append the json object in my array field of collection but i am getting this error "error: Error generating response. ParseError { code: 101, message: 'Object not found.' } code=101, message=Object not found.". I tried add(), addUnique() and set() all these methods but get the same result.

I am sharing my cloud code.
Parse Server - 2.3.8
Nodejs - 6.10.2 Mongodb - 3.4.

var Offer = new Parse.Query(Parse.Object.extend(GameConstants.OFFER));
        Offer.select("collected");
        Offer.equalTo(GameConstants.OBJECT_ID, inputData.offer_id);
        Offer.first({
            success: function (offer) {
                if (typeof offer !== 'undefined') {                    
                    var collected = offer.get(GameConstants.COLLECTED);                    
                    collected.push({user_id: inputData.user_id, date_time: new Date()});                                        
                    offer.set(GameConstants.COLLECTED, collected);//{user_id: inputData.user_id, date_time: new Date()}
                    offer.save(null, {
                        success: function (offer) {
                            var GameUser = new Parse.Query(Parse.Object.extend(GameConstants.GAME_USERS));
                            GameUser.select("coins", "collected_offer");
                            GameUser.equalTo(GameConstants.OBJECT_ID, inputData.user_id);
                            GameUser.first({
                                success: function (gameUser) {
                                    if (typeof gameUser !== 'undefined') {
                                        gameUser.increment(GameConstants.COINS, inputData.coins);
                                        gameUser.addUnique(GameConstants.COLLECTED_OFFERS, {offer_id: inputData.offer_id, offer_coins: inputData.coins, date_time: new Date()});
                                        gameUser.save(null, {
                                            success: function (gameUser) {
                                                callback(null, 1);
                                            },
                                            error: function (error) {
                                                callback(error);
                                            }
                                        });
                                    } else {
                                        callback(null, 2);
                                    }
                                },
                                error: function (error) {
                                    callback(error);
                                }
                            });
                        },
                        error: function (error) {
                            callback(error);
                        }
                    })
                } else {
                    callback(null, 2);
                }
            },
            error: function (error) {
                //Error
                callback(error);
            }
        });

Please help me where i am going wrong. How can i push my custom json objects in array field of mongodb using parse server.

@flovilmart
Copy link
Contributor

object not found probably means that you can't access that object for write because of the ACL. Use either {useMasterKey: true} on save or the user's sessionToken.

@zack1991
Copy link
Author

zack1991 commented May 22, 2017

Hi Flovilmart, thank you for your reply. I am calling this method after succesfully login from parse. I want to apply ACL on this Object because it contains secure data.

I am sharing my current code in which gameUser.save() is working fine but offer.save() is not.

`collectOffer: function (inputData, callback) {
        var Offer = new Parse.Query(Parse.Object.extend(GameConstants.OFFER));
        Offer.select("coins","collected");
        Offer.equalTo(GameConstants.OBJECT_ID, inputData.offer_id);
        Offer.first({
            success: function (offer) {
                if (typeof offer !== 'undefined') {
                    var GameUser = new Parse.Query(Parse.Object.extend(GameConstants.GAME_USERS));
                    GameUser.select("coins", "collected_offer");
                    GameUser.equalTo(GameConstants.OBJECT_ID, inputData.user_id);
                    GameUser.first({
                        success: function (gameUser) {
                            if (typeof gameUser !== 'undefined') {
                                gameUser.increment(GameConstants.COINS, offer.get(GameConstants.COINS));
                                gameUser.addUnique(GameConstants.COLLECTED_OFFERS, {offer_id: offer.id, coins: offer.get(GameConstants.COINS)});
                                gameUser.save(null, {
                                    success: function (gameUser) {
                                        var obj = offer.toJSON();
                                        offer.set(GameConstants.COINS, 5000);
                                        //offer.addUnique(GameConstants.COLLECTED, gameUser.id);
                                        offer.save(null, {
                                            success: function (offer) {
                                                callback(null, 1);
                                            },
                                            error: function (error) {
                                                callback(error);
                                            }
                                        });                                        
                                    },
                                    error: function (error) {
                                        callback(error);
                                    }
                                });
                            } else {
                                callback(null, 2);
                            }
                        },
                        error: function (error) {
                            callback(error);
                        }
                    });
                } else {
                    callback(null, 2);
                }
            },
            error: function (error) {
                //Error
                callback(error);
            }
        });
    }`

@flovilmart
Copy link
Contributor

flovilmart commented May 22, 2017

try:

/// req is the 1st param of you cloud hook
// will be OK if the call is authenticated (for a user)
// req.user may be undefined, if an non auth'd user made the call
let sessionToken = req.user.getSessionToken(); 

offer.save(null, {
  sessionToken: sessionToken,
  success: function (offer) {
    callback(null, 1);
  },
  error: function (error) {
    callback(error);
   }
 });   

@zack1991
Copy link
Author

zack1991 commented May 22, 2017

I got the solution, the error was with my "_id". My "_id" is in integer format and when fetching data from collection it converts my integer id to string format. When try to call save method then the id does not match in the collection, thats why getting that issue.

Can you please help me why parse convert my integer id to string when fetching data.

@flovilmart
Copy link
Contributor

As this is a code related issue, and not an issue with parse-server itself, you should probably as over on stackoverflow. I don't see any _id in your code, and it seems that you have many issues, that don't point to a parse-server specific error. There's no reason why a column would change type, unless your ID column is defined as a string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants