[英]What is the best way to write it? Node.JS
this is my first post: ) Someone can help me to improve my code to get a better way to write it?这是我的第一篇文章:)有人可以帮助我改进我的代码以获得更好的编写方式吗?
Temporada.findAll({
where: { status: 1 }
}).then(temporadas => {
Temporada.count({
where: { status: 1 }
}).then(temporadasAbertas => {
if (temporadasAbertas > 0) {
if (user_logged_acesso == 1) {
JogoContagem.findAll({
where: {
j1Id: user_logged_id,
status: { [Op.ne]: 0 }
}
}).then((blockeds) => {
console.log(blockeds)
res.render("painel/jogos/new", { users, user_logged_id, temporadas, blockeds });
})
} else {
req.flash("error_msg", "No momento você não tem premissão de cadastrar jogos.");
res.redirect("/painel");
}
} else {
req.flash("error_msg", "No momento não há temporadas em aberto para cadastar jogos.");
res.redirect("/painel");
}
})
})
It's working well, however I know that exist a better way to write it, maybe using functions with async await, but I don't know how to collect the results and insert the results to res.render
.它运行良好,但是我知道存在更好的编写方法,可能使用带有异步等待的函数,但我不知道如何收集结果并将结果插入res.render
。 I guess I need to create a var with array... Anyway I suppose it... If you know a way let me know to I improve it.我想我需要用数组创建一个 var ......无论如何我想......如果你知道一种方法让我知道我会改进它。
Observation: My code have a few parts written in English and Portuguese, just if someone read and be curious to know which language.观察:我的代码有几部分是用英语和葡萄牙语编写的,就像有人阅读并想知道哪种语言一样。 ( \o/ Hello from BRASIL ) (\o/来自巴西的你好)
thanks a lot,多谢,
Kind regards Davi Soares亲切的问候戴维苏亚雷斯
This is how you could do that with await
.这就是你可以用await
做到这一点的方法。 Inside an async
function, do:在async
function 中,执行以下操作:
const temporadas = await Temporada.findAll({
where: { status: 1 }
});
const temporadasAbertas = await Temporada.count({
where: { status: 1 }
});
if (temporadasAbertas <= 0) {
req.flash("error_msg", "No momento não há temporadas em aberto para cadastar jogos.");
res.redirect("/painel");
return;
}
if (user_logged_acesso !== 1) {
req.flash("error_msg", "No momento você não tem premissão de cadastrar jogos.");
res.redirect("/painel");
return;
}
const blockeds = await JogoContagem.findAll({
where: {
j1Id: user_logged_id,
status: {
[Op.ne]: 0
}
}
});
res.render("painel/jogos/new", { users, user_logged_id, temporadas, blockeds });
Make sure to .catch
errors too, either in a catch
of a try
/ catch
, or in a .catch
onto the function call.确保.catch
错误,无论是在try
/ catch
的catch
中,还是在.catch
调用上的 .catch 中。
Updated code:更新代码:
router.get("/painel/jogos/new", authenticator, (req, res) => {
var json = req.session.user;
var user_logged = JSON.stringify(json);
var user_logged = JSON.parse(user_logged);
var user_logged_id = user_logged.id;
var user_logged_acesso = user_logged.acesso;
async function load() {
try {
const loadUsers = await User.findAll({ where: { acesso: 1 }, order: [ ['primeiro_nome' , 'ASC'] ] })
const loadTemporadas = await Temporada.findAll({ where: { status: 1 } })
if (loadTemporadas[0] == null) { req.flash("error_msg", "No momento não há temporadas em aberto para cadastar jogos."); res.redirect("/painel"); }
const loadBlockeds = await JogoContagem.findAll({ where: { j1Id: user_logged_id, status: { [Op.ne]: 0 } } })
res.render("painel/jogos/new", { users: loadUsers, user_logged_id, temporadas: loadTemporadas, blockeds: loadBlockeds });
} catch (err) {
console.log(err)
req.flash("error_msg", "Ocorreu algum erro ao carregar a página, favor informar o administrador do site. ERROR CODE: /painel/jogos/new - LOAD");
res.redirect("/painel");
}
}
load()
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.