2011-03-29 154 views
1

這是一個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'); 
       } 
      }); 
+0

是'不'有效的消息類型?改爲嘗試'錯誤'。 – generalhenry 2011-03-29 01:24:31

+0

任何東西都是有效的消息類型 – masylum 2011-03-29 09:03:35

+0

你可以在'/ documents'處執行'req.session'的'console.log'嗎? – masylum 2011-03-29 09:07:47

回答

0

catch該塊既包含錯誤消息,並且「壞JSON」記錄器,因此它們將總是發生在同一時間,由於塊範圍。

相關問題