I am currently working on a Node code that responds when a promise is resolved through the function multicreation.userCreation():
const express = require("express")
const app = express()
const csv=require('csvtojson')
const multer = require("multer")
const csvfilename = `Users-${Date.now()}.csv`
const multiUserCreation = require("./modules/fbadmin")
const multicreation = new multiUserCreation()
const upload = multer({
storage: storage,
limits: { fileSize: 1e6}
}).single("usersdata")
app.post("/uploadCSV",function (req, res, next) {
upload(req,res,function(err) {
if(err) {
res.send(err)
}
else {
const converter=csv()
.fromFile(`./temp/${csvfilename}`)
.then((json)=>{
res.send(multicreation.userCreation(json))
})
}
})
}
On the other hand, below is the code for the "multiuserCreation" class:
const admin = require("firebase-admin");
require("dotenv").config();
class multiUserCreation {
userCreation(jsonUsers) {
if (!admin.apps.length) {
admin.initializeApp({
credential: admin.credential.cert(),
databaseURL: `https://${process.env.PROJECT_ID}.firebaseio.com/`,
});
}
const db = admin.firestore();
async function insertUsers(jsonUsers) {
let messages = [];
const users = jsonUsers
for (let i = 0; i < jsonUsers.length; i++) {
const message = await admin
.auth()
.createUser({
email: jsonUsers[i]["email"],
emailVerified: false,
password: "password",
disabled: false,
})
.then(function (userRecord) {
return {
"User email": jsonUsers[i]["email"],
Result: "Successfully created",
};
})
.catch(function (error) {
return { "User email": jsonUsers[i]["email"], Result: error.code };
});
messages.push(message);
}
return messages;
}
const messageFinal = insertUsers(jsonUsers);
messageFinal.then(function (result) {
return messageFinal;
});
}
}
module.exports = multiUserCreation;
Currently, even though the "messages" array is being populated successfully, multiUserCreation does not seem to be returning anything to the main code. Thank you for your assistance.