I have a nodejs script allowing me to insert data contained in json files on Firestore, but after execution I have a function not found error. How can I fix this situation? Thanks Here is all my code:
var admin = require("firebase-admin");
var serviceAccount = require("./service_key.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://xxxxxxxxx.firebaseio.com"
});
const firestore = admin.firestore();
const path = require("path");
const fs = require("fs");
const directoryPath = path.join(__dirname, "files");
fs.readdir(directoryPath, function(err, files) {
if (err) {
return console.log("Unable to scan directory: " + err);
}
files.forEach(function(file) {
var lastDotIndex = file.lastIndexOf(".");
var menu = require("./files/" + file);
menu.forEach(function(obj) {
firestore
.collection(file.substring(0, lastDotIndex))
.doc(obj.itemID)
.set(obj)
.then(function(docRef) {
console.log("Document written");
})
.catch(function(error) {
console.error("Error adding document: ", error);
});
});
});
});
I have this mistake to send back:
/Users/nana/Documents/SCRIPT/uploader.js:25
menu.forEach(function(obj) {
^
TypeError: menu.forEach is not a function
at /Users/nana/Documents/SCRIPT/uploader.js:25:10
at Array.forEach (<anonymous>)
at /Users/nana/Documents/SCRIPT/uploader.js:20:9
at FSReqCallback.oncomplete (node:fs:188:23)
From what I can tell menu
isn't an array. You are simply requiring
a file inside the path of './files/'
.
Try console.log(typeof menu)
to see what the terminal says menu is. But to me it doesn't look like an array.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.