为什么我的req.flash不能工作?
问题描述:
这是一个Node.js函数。它的工作原理是,糟糕的JSON数据被踢掉了,但它也会闪烁失败的消息。为什么?为什么我的req.flash不能工作?
// Create document
app.post('/documents.:format?', loadUser, function(req, res) {
/////////////////////////added by adam
//tests to see if the inputed text is valid JSON data
data = req.body.d.data;
console.log("///////////" + data);
try {
type = JSON.parse(data);
console.log(type);
} catch (ex) {
console.log("bad json: "+data);
req.flash('Nope', 'Invalid JSON');
res.redirect('/documents');
return;
}
var d = new Document(req.body.d);
d.user_id = req.currentUser.id;
d.save(function() {
switch (req.params.format) {
case 'json':
res.send(d.toObject());
break;
default:
req.flash('info', 'Document created');
res.redirect('/documents');
}
});
答
catch
该块既包含错误消息,并且“坏JSON”记录器,因此它们将总是发生在同一时间,由于块范围。
是'不'有效的消息类型?改为尝试'错误'。 – generalhenry 2011-03-29 01:24:31
任何东西都是有效的消息类型 – masylum 2011-03-29 09:03:35
你可以在'/ documents'处执行'req.session'的'console.log'吗? – masylum 2011-03-29 09:07:47