Why does this code give undefined error? firebase cloud function

Viewed 979

I have this code for retriving document list from firebase collections and processing it to a new list.

But it gives error message:

Error: Update() requires either a single JavaScript object or an alternating list of field/value pairs that can be followed by an optional precondition. Value for argument "dataOrField" is not a valid Firestore value. Cannot use "undefined" as a Firestore value (found in field "myPick"). If you want to ignore undefined values, enable ignoreUndefinedProperties.

What seems to be the problem?

ps - when I test-called 'currentFoodPickList[0].foodCondition', it gave a correct value. so it seems something went wrong while processing 'finalFoodList'

ps2 - new error message after using ignoreUndefinedProperties setting:

Error: Update() requires either a single JavaScript object or an alternating list of field/value pairs that can be followed by an optional precondition. Value for argument "dataOrField" is not a valid Firestore value. "undefined" values are only ignored in object properties.

ps3 - If someone could suggest any other way of processing 'currentFoodPickList' => 'finalFoodList', I'd really appreciate it.

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp();
admin.firestore().settings({ ignoreUndefinedProperties: true })


exports.onFoodDocumentUpdate = functions.firestore.document('foods/{foodID}')
.onUpdate(async (change, context) => {

    const foodDataAfter = change.after.data();
    const currentFoodID = context.params.foodID

    const currentFoodPickList = await admin.firestore().collection('foods').doc(currentFoodID).collection('foodPickRecord').get().then(function(docs) {
        let res = []
        docs.forEach(doc => res.push(doc.data()));
        return res;
    })


    const finalFoodList = [];

    for (var f in currentFoodPickList) {
        for (var i = 0; i < f.amount; i++) {
            finalFoodList.push(f.foodCondition)
        }
    }

    return admin.firestore().collection('foods').doc(currentfoodID).update({
        myPick: finalFoodList[0]
    });
});
0 Answers
Related