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

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

發(fā)送請求后 Firebase http 無法設(shè)置標(biāo)頭

發(fā)送請求后 Firebase http 無法設(shè)置標(biāo)頭

Qyouu 2023-04-20 10:05:39
我正在創(chuàng)建一個(gè)與 API 聯(lián)系的 HTTP 可調(diào)用函數(shù)。但是,發(fā)回 200 時(shí)出現(xiàn)錯(cuò)誤。我認(rèn)為這與我在使用異步函數(shù)時(shí)犯的錯(cuò)誤有關(guān)。這是我的代碼。exports.organisationDataToTemp = functions.region('europe-west3').https.onRequest((req, res) => {  res.set('Access-Control-Allow-Origin', '*');  const GETparam = req.query.kvk;  const KvK = GETparam.toString();  //Test if KvK number is already in temp collection  const snapshot = db.collection('temp').where('information.kvkNumber', '==', KvK).get()    .then(function(querySnapshot) {      querySnapshot.forEach(function(doc) {        //This is where it needs to send the header and it fails. I do get here only when I need this code to run, but it still fails        console.log('kvk number already in temp collection');        res.status(200).send(doc.id);        return;      });    });  //Irrelevant code  //Make API call using the provided KvK number  const keyName = 'VitozFMIS';  const API_key = 'sdfghjqwertyuiopdfghytrdcvbjftyujnbvc';  //Call the first JSON  const _EXTERNAL_URL = 'https://api.kvk.nl/api/v2/testprofile/companies?kvkNumber=' + KvK + '&' + keyName + '=' + API_key + '&startPage=1';  fetch(_EXTERNAL_URL)      .then(response => response.json())      .then(data => {        const total = data.data.totalItems;        for(n=1;n<=total;n++){          const API = 'https://api.kvk.nl/api/v2/testprofile/companies?kvkNumber=' + KvK + '&' + keyName + '=' + API_key + '&startPage=' + n;          //irrelevant code                  //Make the API call          fetch(API)            .then(resp => resp.json())            .then(data => {                //irrelevant code            });        }  });  //Return 200 if no errors occured  res.status(200).send(cleanupID);  return;});通常,代碼完全按需要運(yùn)行,但是當(dāng) kvk 號已經(jīng)在集合中時(shí),它需要將文檔 ID 發(fā)送回 200。我不確定,但我認(rèn)為這是因?yàn)樗谶@個(gè)代碼之前發(fā)送了另一個(gè)代碼,但我不明白為什么會(huì)失敗。有人知道什么失敗了嗎?
查看完整描述

1 回答

?
隔江千里

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

在發(fā)生任何其他事情之前,這行代碼總是會(huì)立即返回 200 響應(yīng):

res.status(200).send(cleanupID);

那是因?yàn)槟褂玫?Firestore 和 fetch API 是異步的并且會(huì)立即返回。您提供的回調(diào)會(huì)在結(jié)果可用后的一段時(shí)間后執(zhí)行。調(diào)用then不會(huì)阻止您的代碼繼續(xù) - 它只是建立一個(gè)回調(diào),以便在 promise 解決時(shí)運(yùn)行。

Cloud Functions 要求發(fā)送響應(yīng)必須是您的函數(shù)所做的最后一件事,因此您應(yīng)該等到所有工作完全成功或失敗后再發(fā)送響應(yīng)。

您將需要構(gòu)建您的代碼以使用承諾中的回調(diào)來決定要做什么。所有的邏輯大部分都在回調(diào)中,或者通過返回另一個(gè)承諾推遲到另一個(gè)回調(diào)。


查看完整回答
反對 回復(fù) 2023-04-20
  • 1 回答
  • 0 關(guān)注
  • 115 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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