Do yourself a favor, and instal Bun (a replacement for NodeJS) and run it with Bun
import { initializeApp } from "firebase/app";
import { getFirestore, collection, getDocs, addDoc } from 'firebase/firestore/lite';
const firebaseConfig = {
// INSERT YOUR OWN 7 lines here !!!! :)
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
// Get a list of recipes from your database
async function getRecipes(db) {
const recipesCol = collection(db, 'recipes');
const recipeSnapshot = await getDocs(recipesCol);
const recipeList = recipeSnapshot.docs.map(doc => doc.data());
return recipeList;
}
function show_recipes(recipes) {
console.log("show_recipes");
if (!recipes || recipes.length == 0) {
console.log("no recipes");
return;
}
// iterate thru recipes, spew name, title
for (const recipe of recipes) {
console.log(recipe.author, recipe.title);
}
console.log("done with show_recipes");
}
getRecipes(db).then((recipes) => {
show_recipes(recipes);
});
async function addRecipe(db, recipe) {
const recipesCol = collection(db, 'recipes');
const recipeSnapshot = await addDoc(recipesCol, recipe);
return recipeSnapshot;
}
// random integer 100 to 999:
const rrrrr = Math.floor(Math.random() * 900) + 100;
const myRandString = rrrrr.toString();
const newRecipe = {
author: "tom" + myRandString,
title: "spaghetti and meatballs " + myRandString
};
addRecipe(db, newRecipe).then((recipe) => {
console.log("... created");
console.log(recipe.author, recipe.title);
console.log(" done with created. for ", myRandString);
});
async function addAndShowRecipe(db, newRecipe) {
await addRecipe(db, newRecipe);
const recipes = await getRecipes(db);
show_recipes(recipes);
}
addAndShowRecipe(db, newRecipe).then(() => {
console.log("Done adding and showing recipe for ", myRandString);
});
Now run it using a decent node.js replacement... bun
bun run index.js