json - 为什么我的 req.flash 不起作用?

标签 json parsing node.js exception try-catch

这是一个 Node.js 函数。它可以工作,因为坏的 JSON 数据会被踢掉,但它也会闪烁失败的消息。为什么?

// 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 block 包含错误消息和“错误 JSON”记录器,因此由于 block 作用域,它们总是同时发生。

关于json - 为什么我的 req.flash 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5466480/

相关文章:

javascript - 使用回调函数更新 JSON 对象

php - 在 php laravel 中将 html 转换为 json

android - 无法解析 Titanium 中的推送有效负载消息

parsing - 函数定义中的相互引用

c++ - 简单解析器的简单方法

node.js - 无法访问 Kubernetes 集群上的本地证书

ssl - Cloud Foundry 上的 HTTPS Node 应用程序

java - 如何通过 key 从 JSON 中获取值?

jquery - 生成的 JsonResult 中的属性名称大小写不一致

javascript - jQuery Mobile、NPM 和 WebPack 的问题