I am trying to add some dummy data to firebase firestore but only the setDoc command is running twice. I don’t quite understand what I’m doing wrong. This is only a .ts file, not a component file & the function is not running twice. I am sure of that. Below is my code:
import { collection, setDoc, Timestamp, doc, addDoc } from "firebase/firestore";
import { db } from "./clientApp";
type MyBrand = { name: String; image: String; dateAdded: Timestamp };
var brandCollRef = collection(db, "brand");
async function addBrands() {
let brands: Array<MyBrand> = [];
for (var i = 1; i <= 10; i++) {
var myItem = {
name: `Brand ${i}`,
image: `https://picsum.photos/1024/1024?random=${i}`,
dateAdded: Timestamp.now(),
};
brands.push(myItem);
console.log("Added Item: " + myItem.name);
}
brands.forEach(async (item) => {
// New Doc created
const newDoc = doc(brandCollRef);
console.log("New Doc Created: " + newDoc.id);
// This code is running twice
await setDoc(newDoc, item);
console.log("Set Data in: " + newDoc.id);
});
}
export { addBrands };
Please let me know what I’m doing wrong.