3 回答

TA貢獻(xiàn)1773條經(jīng)驗(yàn) 獲得超3個(gè)贊
我已經(jīng)可以看到你正在使用body-parser或其他類型的請(qǐng)求解析器(顯然),所以你可以編寫一個(gè)單行實(shí)用程序來(lái)執(zhí)行此操作
## req.body = {a: 1, b:2, c:3, d:4}
let targetObj = {}
let keysYouWant = ['key1', 'key2']
Object.keys(req.body).forEach(key => { if (keysYouWant.includes(key)) targetObj[key] = req.body[key] })
但I(xiàn)F req.body具有與targetObject中相同的鍵,那么您可以這樣做
return BSRequest.create(req.body)
.then((bsRequest) => { ... })
.catch((error) => { ... })

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超6個(gè)贊
是的,我們有es6傳播功能來(lái)執(zhí)行此操作。請(qǐng)檢查此鏈接
注意:內(nèi)部的所有值req.body
都將被傳遞
return BSRequest.create({...req.body}).then(bsRequest => res.status(201).send(bsRequest)).catch(error => res.status(400).send(error));

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊
如果req.body和你的數(shù)據(jù)庫(kù)db模型鍵是相同的,你可以寫
return BSRequest.create(req.body).then(bsRequest => res.status(201).send(bsRequest)) .catch(error => res.status(400).send(error));
評(píng)論:我不建議您將req.body中的值直接存儲(chǔ)到數(shù)據(jù)庫(kù)中。請(qǐng)?jiān)趯?code>type-check前端保存到數(shù)據(jù)庫(kù)之前包含從前端獲取的值。你可以通過(guò)在Akshay的答案中做一些修改來(lái)做到這一點(diǎn)。
添加回答
舉報(bào)