I've been through the different topics related to my issue but couldn't fix it. My firebase cloud function with the onCreate trigger does not deploy. I get this error : Failed to load function definition from source: Failed to generate manifest from function source: SyntaxError: await is only valid in async functions and the top level bodies of modules
// add action to users when they have the objective already added
exports.addActionToUserWhenCreated = functions.firestore
.document('actions/{documentUid}')
.onCreate(async (snap, context) => {
// get the objectives
let actionObjectives = snap.data().objectives
let actionId = snap.id
let objSelectedBy = []
// For each objective, get the selectedBy field
actionObjectives.forEach(objective => {
const docSnap = await db.doc(`objectives/${objective}`).get()
objSelectedBy = docSnap.data().selectedBy
objSelectedBy.forEach(user => {
// Add the actionId to the user's selectedActions
db.doc(`users/${user}/selectedActions/${actionId}`).set({
achievedRate: 0,
})
})
// Add all the userIds to the action's selectedBy field
db.doc(`actions/${actionId}`).update({
selectedBy: objSelectedBy,
}, {merge: true});
})
return;
});
Do you see the problem? Thanks! Max