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

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

Firebase“未處理的拒絕”和“設(shè)置標(biāo)頭后無法設(shè)置標(biāo)頭”JavaScript 錯(cuò)誤

Firebase“未處理的拒絕”和“設(shè)置標(biāo)頭后無法設(shè)置標(biāo)頭”JavaScript 錯(cuò)誤

ITMISS 2022-06-09 10:47:54
首先,請注意,我對 JS 和編碼非常陌生 :)期望的行為:我編寫了以下 JS HTTPS Firebase 函數(shù),該函數(shù)接受查詢參數(shù)locationId,執(zhí)行 GET API 調(diào)用并將響應(yīng)保存回 Firebase。該代碼根據(jù)需要正確地將數(shù)據(jù)保存到 Firebase。我遇到過類似的問題,但我正在努力使這些解決方案適應(yīng)我下面的具體問題。據(jù)我所知,我只發(fā)送一次響應(yīng)。具體錯(cuò)誤:以下是控制臺(tái)輸出發(fā)送到客戶端后無法設(shè)置標(biāo)頭未處理的拒絕
查看完整描述

1 回答

?
翻閱古今

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

通過展平嵌套的 Promise,您可以看到您的代碼正在執(zhí)行以下指令(當(dāng)axios調(diào)用未引發(fā)錯(cuò)誤時(shí)):


admin.database().ref(`/venue-menus/${locationId}/`).set(response.data))

  .then(response => res.status(200).send(locationId))

  .catch(err => res.status(500).send({error: err})

  .then(response => res.status(200).send(locationId)) // this line is always called after either of the above.

  .catch(err => res.status(500).send({error: err})

作為一般做法,除非需要,否則不應(yīng)將 promise 與它們自己的then()和catch()處理程序嵌套,因?yàn)樗鼤?huì)導(dǎo)致像這樣的奇怪效果。


此外,如果您的代碼需要使用//end axios或//end cors消息,您應(yīng)該扁平化您的代碼,以便在沒有這些消息的情況下有意義。


將您的代碼調(diào)整為“快速失敗”,更正您的 API 響應(yīng)并適當(dāng)隱藏錯(cuò)誤堆棧跟蹤可以提供:


const cors = require('cors')({

  origin: true,

  methods: ["GET"]

});



exports.doshiiGetMenuForOnboardedVenue = functions.https.onRequest((req, res) => {

  cors(req, res, (err) => { // note: cors will handle OPTIONS method


    if (err) {

      // note: log full error at ERROR message level

      console.error('Internal CORS error:', err);

      // note: return only generic status message to client

      return res.status(500).json({error: 'Internal Server Error'});

    }


    // Forbidding anything but GET requests.

    if (req.method !== 'GET') {

      // 405 METHOD_NOT_ALLOWED

      return res.status(405)

        .set('Allow', 'GET')

        .json({error: 'Not Allowed!'});

    }


    const locationId = req.query.locationId;


    console.log('locationId', locationId);


    if (!locationId) {

      // 400 BAD_REQUEST

      return res.status(400).json({error: 'locationId missing'})

    }


    var token = jwttoken();


    const options = {

        headers: {

          'content-type': 'application/json',

          'authorization': 'Bearer ' + token

        }

      };


    // note: Don't forget to enable billing for third-party APIs!

    const uri = 'https://sandbox.doshii.co/partner/v3/locations/' + locationId + '/menu?lastVersion=:lastVersion&filtered=true'


    axios.get(uri, options)

      .then(response => admin.database().ref(`/venue-menus/${locationId}/`).set(response.data))

      .then(() => {

        // note: as locationId was already sent by the client, send new/useful

        // information back or nothing but the right status code

        res.status(200).json({ ref: `/venue-menus/${locationId}/` });

      })

      .catch(err => {

        // note: log full error at ERROR message level

        console.error('Failed to retrieve/save API data:', err);

        // note: return only message to client

        res.status(500).json({error: err.message || 'Internal Server Error'});

      });

  });

});


查看完整回答
反對 回復(fù) 2022-06-09
  • 1 回答
  • 0 關(guān)注
  • 112 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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