我正在使用nodejs
这是我的函数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();我正在调用这个函数,在这个post函数中
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);
});
};我在第二个post函数createBusiness,中调用第一个函数
我想完整地完成createSchema函数,然后只需要在我的第二个函数createSchema中执行其他的然后方法()
看到我的代码,我做了一个需要首先工作的评论,
我尝试了异步等待,但没有工作!
发布于 2018-06-07 10:13:31
你错过了很多地方的回报承诺。你得把它们都还给我:
// No "block" implies return
const createSchema = () =>
Business.findAll({ raw: true})
.then((data) =>
// wrap Promise.all and map() instead of forEach()
Promise.all(
data.map((client) =>
postgresDB.createSchema(client.code).then(() =>
// Again wrap Promise.all and map()
Promise.all(
Object.keys(postgresDB.models).map((currentItem) =>
postgresDB.models[currentItem].schema(client.code).sync()
)
)
)
)
)
)
.then(() => console.log("now I'm done"))
//.catch((err) => console.log('Warning:', err.message));因此,主要是包装Promise.all并使用Array.map()实际返回您正在迭代的承诺。
另一件事是不要过度使用块{}。只要返回箭头函数,当你只有一件东西在那里无论如何。可以选择删除.catch(),只允许抛出此函数中的错误。调试之后,您实际上“应该”删除该行并允许抛出错误。
https://stackoverflow.com/questions/50738207
复制相似问题