2016-09-23 129 views
1

我要發佈嵌套支柱件的NodeJS - 快 - 嵌套後數據未解析正確

查看HTML形式

<form method="post" action=""> 
<input type="text" placeholder="Enter Tag here" class="gui-input" name="reply_message"> 
<input type="text" placeholder="Enter Label for Decision" class="gui-input" name="decision[0][label]" value="Interested"> 
<input type="text" placeholder="Enter decision keywords here" class="gui-input" data-role="tagsinput" name="decision[0][keyword]" value="Interested, call me"> 
<input type="text" placeholder="Enter Label for Decision" class="gui-input" name="decision[1][label]" value="Not Interested"> 
<input type="text" placeholder="Enter decision keywords here" class="gui-input" data-role="tagsinput" name="decision[1][keyword]" value="not interested, not"> 
<input type="text" placeholder="Enter Label for Decision" class="gui-input" name="decision[2][label]" value="Call Later" > 
<input type="text" placeholder="Enter decision keywords here" class="gui-input" data-role="tagsinput" name="decision[2][keyword]" value="Interested, call me, later"> 
<button class="button btn-primary" type="submit">Submit </button> 

路由器文件

router.post('/mapping', isLoggedIn, function (req, res, next) { 
    var posted_data= req.body; 
    console.log(req.body); 
    res.send(posted_data); 
}); 

我得到像這樣的發佈數據結構

{ 
    "reply_message": "This is test", 
    "decision[0][label]": "Interested", 
    "decision[0][keyword]": "Interested, call me", 
    "decision[1][label]": "Not Interested", 
    "decision[1][keyword]": "not interested, not", 
    "decision[2][label]": "Call Later", 
    "decision[2][keyword]": "Interested, call me, later" 
} 

但實際發佈的數據結構應該是

{ 
    "reply_message": "This is test", 
    "decision": [{ 
     "label": "Interested", 
     "keyword": "Interested, call me" 
    }, { 
     "label": "Not Interested", 
     "keyword": "not interested, not" 
    }, { 
     "label": "Call Later", 
     "keyword": "Interested, call me, later" 
    }] 
} 

因此,如何CA我做到這一點,是否有任何節點模塊我必須使用像這樣發佈表單數據?

+1

您使用的是舊版本Express和/或'身體parser'的? 'body-parser'的默認配置會自動解析該輸入。請參閱['extended'選項](https://www.npmjs.com/package/body-parser#extended)。 – robertklep

+0

我正在使用最新版本的兩個...我必須使用擴展選項來以嵌套方式解析。 –

+0

我正在鏈接的那個,這是默認的。但是,由於表單的「操作」是空的,我假設您正在使用某種客戶端代碼來提交數據?你確定這樣做沒問題嗎?例如,你沒有提交任何機會的數據作爲JSON? – robertklep

回答

1

那麼,name="decision[0][label]"工作正常。表單數據作爲鍵值對提交,輸入名稱成爲關鍵字。

如果使用HTTP GET提交表單,則會在req.query中獲得所需的對象。但對於HTTP POST,它按原樣出現在req.body中。

這裏,qs module可以幫你在服務器端:

const qs = require('qs'); 

const input = { 
    "reply_message": "This is test", 
    "decision[0][label]": "Interested", 
    "decision[0][keyword]": "Interested, call me", 
    "decision[1][label]": "Not Interested", 
    "decision[1][keyword]": "not interested, not", 
    "decision[2][label]": "Call Later", 
    "decision[2][keyword]": "Interested, call me, later" 
}; 

const output = qs.parse(qs.stringify(input)); 

console.log(output); 

// console: 
{ reply_message: 'This is test',                         
    decision:                              
    [ { label: 'Interested', keyword: 'Interested, call me' },                 
    { label: 'Not Interested', keyword: 'not interested, not' },                
    { label: 'Call Later', keyword: 'Interested, call me, later' } ] } 
+0

感謝您的幫助...其工作...: ) –