第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

將req.body屬性映射到對(duì)象鍵

將req.body屬性映射到對(duì)象鍵

BIG陽(yáng) 2019-04-17 13:15:26
現(xiàn)在,當(dāng)我想將對(duì)象添加到數(shù)據(jù)庫(kù)時(shí),我執(zhí)行以下操作:return BSRequest.create({             salon_name: req.body.salon_name,             salon_type: req.body.salon_type,             employees: req.body.employees,             postcode: req.body.postcode,             city: req.body.city,             website: req.body.website,             first_name: req.body.first_name,             last_name: req.body.last_name,             email: req.body.email,             phone_number: req.body.phone_number         })             .then(bsRequest => res.status(201).send(bsRequest))             .catch(error => res.status(400).send(error));在Javascript / Node中有沒有辦法自動(dòng)獲取屬性,req.body以便這些屬性可以映射到對(duì)象的鍵?或者其他一些方法,我可以簡(jiǎn)化它并減少代碼。我正在使用sequelize ORM。
查看完整描述

3 回答

?
慕容3067478

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) => { ... })


查看完整回答
反對(duì) 回復(fù) 2019-05-17
?
呼啦一陣風(fēng)

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));


查看完整回答
反對(duì) 回復(fù) 2019-05-17
?
吃雞游戲

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)。


查看完整回答
反對(duì) 回復(fù) 2019-05-17
  • 3 回答
  • 0 關(guān)注
  • 623 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)