2017-05-22 73 views
0

我想推或追加json對象在我的數組字段的集合,但我得到這個錯誤「錯誤:錯誤生成響應。ParseError {code:101,message :'未找到對象'。 } code = 101,message =找不到對象。「如何推送解析服務器的數組字段中的JSON對象

我在分享我的雲代碼。 解析服務器 - 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); 
      } 
     }); 

請幫助我在哪裏我錯了。如何使用解析服務器將我的自定義json對象推送到mongodb的數組字段中。

謝謝。

回答

0

@ 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));