I'm building a code in Node which returns a response when a promise is solved (function called: 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))
})
}
})
}
By the other hand, the class "multiuserCreation" code is described as below:
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: "Succesfully 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;
Actually, despite "messages" array is fullfiled succesffuly, multiUserCreation is not returning anything to main code. Thanks for your help
You need to return the promise from multicreation.userCreation Check the change below
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: "Succesfully created",
};
})
.catch(function (error) {
return { "User email": jsonUsers[i]["email"], Result: error.code };
});
messages.push(message);
}
return messages;
}
const messageFinal = insertUsers(jsonUsers);
return messageFinal.then(function (result) {
return messageFinal;
});
}
}
module.exports = multiUserCreation;
Also, while calling the userCreation, you need to handle it in a callback
const converter=csv()
.fromFile(`./temp/${csvfilename}`)
.then((json)=>{
multicreation.userCreation(json).then(function (response) {
res.send(multicreation.userCreation(response))
})
})
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.