I have a collection "Houses" in firebase which is supposed to contain documents. So my function takes the document data and adds it to the given collection name.
If the collection "Houses" does not exist, how do I create a new one programmatically and add data to it? (Some way to check if the collection already exists, and make a new one if it returns false?)
This is my addData function:
exports.addData = functions.https.onCall((data, context) => {
let validated = true;
validateForm(data);
if (validated === false) {
console.log('Data cannot be validated. Misses the correct attributes')
} else {
addAttributes(data);
for (let i=0; i<data.length; i++) {
db.collection('Houses')
.add(data[i])
.then((docRef) => {
console.log('Success ', docRef.id);
return { message: "Done" }
})
.catch(err => {
console.log('Error logged', err);
})
}
}
})