2017-05-22 8 views
0

コレクションの配列フィールドにjsonオブジェクトをプッシュまたは追加しようとしていますが、このエラーが発生しています "エラー:レスポンスの生成中にエラーが発生しました。 : 'オブジェクトが見つかりませんでした。' }コード= 101、メッセージ=オブジェクトが見つかりません。 "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); 
      } 
     }); 

私が間違っているところを助けてください。どのように私はmongodbの配列フィールドで私のカスタムjsonオブジェクトをパースサーバーを使用してプッシュすることができます。

ありがとうございました。

答えて

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)); 
関連する問題