0
votes

I get the error Error in function [undefined:: object is not iterable (cannot read property Symbol(Symbol.iterator))] (I typed out the error in function) when running a User onDelete Firebase Cloud Function, which is a near mirror of my User onCreate function (that works like a charm):

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const FS = admin.firestore();
const FA = admin.auth();

exports.newUser = functions.auth.user().onCreate(async(user)=>{
    const fTime = admin.firestore.Timestamp.now()
    const day = fTime.toDate().toLocaleDateString().replace('/','-').replace('/','-')
    const time = fTime.toDate().toLocaleTimeString('en',{hour12:false})+'.'+fTime.toDate().getMilliseconds()
    try {
        const act1 = FS.doc(`users/${user.uid}`).create({email:user.email})
        const act2 = FS.doc(`globalEvents/${day}`).set({[time]:{time:fTime,source:'Function newUser',description:`Account created for [${user.uid} - ${user.email}]`}},{merge:true})
        const promise = await Promise.all(act1,act2)
        return promise
    }
    catch (err) {
        return FS.doc(`globalEvents/${day}`).set({[time]:{time:fTime,source:'Function newUser',description:`Error in function [${err.code}:: ${err.message}]`}},{merge:true})
    }
})

exports.delUser = functions.auth.user().onDelete(async(user)=>{
    const fTime = admin.firestore.Timestamp.now()
    const day = fTime.toDate().toLocaleDateString().replace('/','-').replace('/','-')
    const time = fTime.toDate().toLocaleTimeString('en',{hour12:false})+'.'+fTime.toDate().getMilliseconds()
    try {
        const act1 = FS.doc(`users/${user.uid}`).delete()
        const act2 = FS.doc(`globalEvents/${day}`).set({[time]:{time:fTime,source:'Function delUser',description:`Account deleted for [${user.uid} - ${user.email}]`}},{merge:true})
        const promise = await Promise.all(act1,act2)
        return promise
    }
    catch (err) {
        return FS.doc(`globalEvents/${day}`).set({[time]:{time:fTime,source:'Function delUser',description:`Error in function [${err.code}:: ${err.message}]`}},{merge:true})
    }
})

For those recognized with Firebase Cloud Functions I have "admin.initializeApp();" in the index.js, and the above in a user.js linked by exports.newUser = users.newUser" & "exports.delUser = users.delUser

Due to the async nature of how I've laid it out (again, the onCreate works perfectly) it will still delete the user's document, but triggers the catch and creates the following document:

12:48:24.981
description
"Error in function [undefined:: object is not iterable (cannot read property Symbol(Symbol.iterator))]"
source
"Function delUser"
time
November 8, 2020 at 7:48:24 AM UTC-5
1

1 Answers

1
votes

This is because Promise.all() accepts a single argument, which must be an iterable of Promises, such as an Array.

Passing an array, as follows, await Promise.all([act1, act2]), instead of doing await Promise.all(act1,act2), will solve the problem.