I am using nodejs
This is my function called createSchema:
const createSchema = () => {
Business.findAll({
raw: true,
}).then((data) => {
data.forEach((client) => {
postgresDB.createSchema(client.code).then(() => {
Object.keys(postgresDB.models).forEach((currentItem) => {
postgresDB.models[currentItem].schema(client.code).sync();
});
console.log('Postgres schema created');
}).catch(() => {
});
});
}).catch((err) => {
console.log('Warning:', err.message);
});
};
createSchema();
I am calling this function, inside this post function
exports.createBusiness = (req, res) => {
const business = {
name: req.body.name,
code: req.body.code,
email: req.body.email,
};
Business.create(business)
.then((rawbusinessData) => {
createSchema() // this is the function
.then(() => { . // i want to complete createSchema fully then only i want to execute this below stuffs
const businessData = rawbusinessData.get({ plain: true });
const loginDetails = {
username: 'sameer',
password: encrypt('sameer'),
};
const schemaLogin = postgresDB.models.login.schema(businessData.code);
schemaLogin.create(loginDetails).then((loginData) => {
console.log('loginData:', loginData);
});
res.status(200).send(businessData);
});
})
.catch((err) => {
console.log('err:', err);
});
};
I am calling the first function inside my second post function called createBusiness,
I want to complete the createSchema function fully, then only i need to execute other then method() in my second function called createBusiness
See my code, i made a comment which needs to work first,
I tried with async await but not working!
return Business.findAll({. Missing returnpostgresDB.models[currentItem].schema(client.code).sync();a synchronous operation or does it return aPromisethat needs to be resolved before running the code on the bottom?