【问题标题】:How to push JSON Object in Array field of parse server如何在解析服务器的数组字段中推送 JSON 对象
【发布时间】:2017-10-22 07:35:57
【问题描述】:

我正在尝试在我的集合数组字段中推送或附加 json 对象,但我收到此错误“错误:生成响应时出错。ParseError { 代码:101,消息:'找不到对象。' } 代码=101,消息=找不到对象。”。

我正在分享我的云代码。 解析服务器 - 2.3.8 节点 - 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);
            }
        });

请帮助我哪里出错了。如何使用解析服务器将我的自定义 json 对象推送到 mongodb 的数组字段中。

谢谢。

【问题讨论】:

    标签: javascript json parse-platform parse-server parse-cloud-code


    【解决方案1】:

    @julien-kode。我(还)没有完整的答案,但我有一个可能会有所帮助的建议。您正在使用旧式成功错误函数。这种异步编码风格的一个大问题是错误很容易被“吃掉”。相反,您想抓住链条的末端。所以这里是你的函数的重写。由于我没有足够的信息来编写工作测试,因此我可能在这里遗漏了一点,但希望您能明白这一点,我们可以将球推进到解决方案?

    const Offer = new Parse.Query(Parse.Object.extend(GameConstants.OFFER))
      .select("collected");
      .equalTo(GameConstants.OBJECT_ID, inputData.offer_id);
      .first()
      .then((offer) => {
        if (typeof offer !== 'undefined') {                    
          const collected = offer.get(GameConstants.COLLECTED); // << -- is Array.isArray(collected) === true ????    
          collected.push({ user_id: inputData.user_id, date_time: new Date() });                                        
          offer.set(GameConstants.COLLECTED, collected); 
          return offer.save(); // <<-- note how i am returning functions that result in promises -- this is the magic!
        } else {
          return callback(null, 2);
        }
      })
      .then((offer) => {
        return new Parse.Query(Parse.Object.extend(GameConstants.GAME_USERS)) // <-- whoa, we're returning the promise that results from the first() call.  cool.
          .select("coins", "collected_offer");
          .equalTo(GameConstants.OBJECT_ID, inputData.user_id);
          .first()
      })
      .then((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()});
            return gameUser.save().then(() => callback(null, 1)) // a single line function with no {} will return the result of the expression....
        } else {
            return callback(null, 2);
        }
      })
      .catch(callback); <-- this is the same as .catch((error) => callback(error));
    

    【讨论】:

      猜你喜欢
      • 2015-03-28
      • 1970-01-01
      • 2016-12-27
      • 2019-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-15
      相关资源
      最近更新 更多