请考虑以下情况。
我在验证表单后传递了来自nodeJS的错误。然后页面重新加载并将错误加载到刷新的页面上。但是,表单本身实际上位于模态内。当在服务器端的res.render()函数中传递错误时,如何打开该模式?
如何根据服务器端的locals变量触发客户端js文件中的函数?
服务器端代码是这样的:
router.post('/create', [
check('company_name').not().isEmpty().withMessage('Company Name must not be empty'),
check('contact_name').not().isEmpty().withMessage('Contact Name must not be empty'),
], (req, res, next) => {
const errors = validationResult(req);
if(!errors.isEmpty()){
req.flash( 'errors', errors.mapped() );
res.redirect('/client');
} else {
console.log('No errors found');
var newClient = new client({
company_name : req.body.company_name ,
contact_name : req.body.contact_name ,
});
newClient.save(function(err){
if (err) throw err;
});
console.log("Client Created!");
req.flash('success_msg', 'You have created a new client!');
res.redirect('/client');
}
});
答案 0 :(得分:0)
你可以在url中传递该变量,然后在init函数左右传递
像:http://yourdomain.com/sompage.whatever?error=yes;
在开始的客户端上,如果验证了某些检查,则在url中检查该变量,然后显示模态。
您可以使用网址查询字符串和网址参数作为了解差异的示例:
What is the difference between URL parameters and query strings?