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

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

NodeJS:更改密碼期間未處理的承諾拒絕警告

NodeJS:更改密碼期間未處理的承諾拒絕警告

慕桂英546537 2021-12-02 15:43:00
我正在 NodeJS 中更改密碼,但在請(qǐng)求處理過(guò)程中出現(xiàn)以下錯(cuò)誤:(node:16220) UnhandledPromiseRejectionWarning: Error: Can't set headers after they are sent.    at validateHeader (_http_outgoing.js:491:11)    at ServerResponse.setHeader (_http_outgoing.js:498:3)    at ServerResponse.header (/home/pbaj/Documents/Projects/syberiaquotes-backend/node_modules/express/lib/response.js:771:10)    at ServerResponse.send (/home/pbaj/Documents/Projects/syberiaquotes-backend/node_modules/express/lib/response.js:170:12)    at ServerResponse.json (/home/pbaj/Documents/Projects/syberiaquotes-backend/node_modules/express/lib/response.js:267:15)    at user.(anonymous function).updateOne.then.catch.err (/home/pbaj/Documents/Projects/syberiaquotes-backend/api/controllers/user.js:284:52)    at <anonymous>    at process._tickCallback (internal/process/next_tick.js:188:7)(node:16220) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)(node:16220) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.在我的changePassword路線(xiàn)中,我采用email,password,newPassword價(jià)值觀和:檢查電子郵件是否存在,如果存在...比較給定的密碼和用戶(hù)密碼,如果它們相同...bcrypt.hash 函數(shù)采用 newPassword,從中生成哈希值,并保存到 mongoDB如果我提供正確的電子郵件和密碼,上面的所有要點(diǎn)都已完成,但我明白了UnhandledPromiseRejectionWarning。我看到這個(gè)問(wèn)題的原因是在user[0].updateOne(user[0])函數(shù)內(nèi)部,正是在,catch()但我不知道發(fā)生了什么。我需要[200]'Password changed!'從我的要求的答案,但我得到[401]'Auth failed'becouse的UnhandledPromiseRejectionWarning。
查看完整描述

2 回答

?
精慕HU

TA貢獻(xiàn)1845條經(jīng)驗(yàn) 獲得超8個(gè)贊

您發(fā)送響應(yīng) 2 次。試試這個(gè)代碼


 if (result) {

            bcrypt.hash(newPassword, 10, (err, hash) => {

                console.log(hash)

                if (err) {

                    return res.status(500).json({

                        error: err

                    })

                } else {

                    user[0].password = hash

                    user[0]

                    .updateOne(user[0])

                    .then(result => {

                        return res.status(200).json({

                            message: 'Password changed!',

                            result: result,

                        })

                    })

                    .catch(err => {

                        res.status(500).json({ message: err.message })

                    })

                }

            })

} else {

          return res.status(401).json({

            message: 'Auth failed'

          })

}


查看完整回答
反對(duì) 回復(fù) 2021-12-02
?
慕萊塢森

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊

我假設(shè)您使用的是貓鼬(因?yàn)?code>User.find(...))。

如果是這樣的話(huà),你不能這樣做user[0].updateOne(...),因?yàn)?code>user[0]已經(jīng)是一個(gè)類(lèi)型的對(duì)象User

您要么執(zhí)行User.findOneAndUpdate(...),要么user[0].save(...)在設(shè)置新密碼后執(zhí)行(這對(duì)我來(lái)說(shuō)似乎更直接)。


查看完整回答
反對(duì) 回復(fù) 2021-12-02
  • 2 回答
  • 0 關(guān)注
  • 300 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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