为什么不在 Node.js Express 项目中对路由使用 .Flash 和 .Redirect Fire?
Why Don't .Flash and .Redirect Fire for Route in a Node.js Express Project?
我正在使用 flash widely in my Node.js Express application, but for one route I can't get flash or redirect to work. Views are with pug:
form.permissive(action=`/setPermissive/${device._id}/${device.allowUnlock}` method="POST")
label.groupTitle(for="setPermissive") Allow Unlock
button.switch(type="submit" name="setPermissive")
label.switch
if (device.allowUnlock)
input(type="checkbox" name="setPermissive" checked)
else
input(type="checkbox" name="setPermissive" )
span.slider
路线:
router.post('/setPermissive/:id/:permissive', catchErrors(deviceController.changePermissive));
控制器:
exports.changePermissive = async (req, res) => {
const permissive = (req.params.permissive === "false") ? false : true;
const device = await Device.findOneAndUpdate({ _id: req.params.id }, { $set: { allowUnlock : !permissive } }, {
new: true
}).exec();
req.flash('success', `Successfully updated ${device.name}`);
res.redirect('/devices');
};
我知道 Device.findOneAndUpdate
执行,因为我可以在后端看到它,但我没有收到闪现消息,我没有被重定向,UI 没有反映更新。如果我手动刷新页面,我会收到闪现消息并且 UI 已更新。
我有类似的路由,可以按预期进行闪烁和重定向,例如:
form.form(action=`/changeStatusEnabled/${device._id}/${newStateEnabled}` method="POST")
input.button(type="submit" value=`${currentStateEnabled}` onclick=`return confirm('${msgConfEnabled}');`)
router.post('/changeStatusEnabled/:id/:state', catchErrors(deviceController.changeStatusEnabled));
exports.changeStatusEnabled = async (req, res) => {
const myBoo = (req.params.state === "enable") ? true : false;
const device = await Device.findOneAndUpdate({ _id: req.params.id }, { $set: { isEnabled: myBoo }}, {
new: true
}).exec();
req.flash('success', `Successfully updated ${device.name}`);
res.redirect(`/devicesAdmin`);
};
我错过了什么?
出于某种原因,.permissive
class 导致打嗝。如果有人知道为什么,我洗耳恭听。
已更改:
form.permissive(action=`/setPermissive/${device._id}/${device.allowUnlock}` method="POST")
label.groupTitle(for="setPermissive") Allow Unlock
button.switch(type="submit" name="setPermissive")
label.switch
if (device.allowUnlock)
input(type="checkbox" name="setPermissive" checked)
else
input(type="checkbox" name="setPermissive" )
span.slider
至:
form(action=`/setPermissive/${device._id}/${device.allowUnlock}` method="POST")
label.groupTitle(for="setPermissive") Allow Unlock
button.switch(type="submit" name="setPermissive")
label.switch
if (device.allowUnlock)
input(type="checkbox" name="setPermissive" checked)
else
input(type="checkbox" name="setPermissive" )
span.slider
我正在使用 flash widely in my Node.js Express application, but for one route I can't get flash or redirect to work. Views are with pug:
form.permissive(action=`/setPermissive/${device._id}/${device.allowUnlock}` method="POST")
label.groupTitle(for="setPermissive") Allow Unlock
button.switch(type="submit" name="setPermissive")
label.switch
if (device.allowUnlock)
input(type="checkbox" name="setPermissive" checked)
else
input(type="checkbox" name="setPermissive" )
span.slider
路线:
router.post('/setPermissive/:id/:permissive', catchErrors(deviceController.changePermissive));
控制器:
exports.changePermissive = async (req, res) => {
const permissive = (req.params.permissive === "false") ? false : true;
const device = await Device.findOneAndUpdate({ _id: req.params.id }, { $set: { allowUnlock : !permissive } }, {
new: true
}).exec();
req.flash('success', `Successfully updated ${device.name}`);
res.redirect('/devices');
};
我知道 Device.findOneAndUpdate
执行,因为我可以在后端看到它,但我没有收到闪现消息,我没有被重定向,UI 没有反映更新。如果我手动刷新页面,我会收到闪现消息并且 UI 已更新。
我有类似的路由,可以按预期进行闪烁和重定向,例如:
form.form(action=`/changeStatusEnabled/${device._id}/${newStateEnabled}` method="POST")
input.button(type="submit" value=`${currentStateEnabled}` onclick=`return confirm('${msgConfEnabled}');`)
router.post('/changeStatusEnabled/:id/:state', catchErrors(deviceController.changeStatusEnabled));
exports.changeStatusEnabled = async (req, res) => {
const myBoo = (req.params.state === "enable") ? true : false;
const device = await Device.findOneAndUpdate({ _id: req.params.id }, { $set: { isEnabled: myBoo }}, {
new: true
}).exec();
req.flash('success', `Successfully updated ${device.name}`);
res.redirect(`/devicesAdmin`);
};
我错过了什么?
出于某种原因,.permissive
class 导致打嗝。如果有人知道为什么,我洗耳恭听。
已更改:
form.permissive(action=`/setPermissive/${device._id}/${device.allowUnlock}` method="POST")
label.groupTitle(for="setPermissive") Allow Unlock
button.switch(type="submit" name="setPermissive")
label.switch
if (device.allowUnlock)
input(type="checkbox" name="setPermissive" checked)
else
input(type="checkbox" name="setPermissive" )
span.slider
至:
form(action=`/setPermissive/${device._id}/${device.allowUnlock}` method="POST")
label.groupTitle(for="setPermissive") Allow Unlock
button.switch(type="submit" name="setPermissive")
label.switch
if (device.allowUnlock)
input(type="checkbox" name="setPermissive" checked)
else
input(type="checkbox" name="setPermissive" )
span.slider