我正在創(chuàng)建一個與 API 聯(lián)系的 HTTP 可調(diào)用函數(shù)。但是,發(fā)回 200 時出現(xiàn)錯誤。我認(rèn)為這與我在使用異步函數(shù)時犯的錯誤有關(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;});通常,代碼完全按需要運行,但是當(dāng) kvk 號已經(jīng)在集合中時,它需要將文檔 ID 發(fā)送回 200。我不確定,但我認(rèn)為這是因為它在這個代碼之前發(fā)送了另一個代碼,但我不明白為什么會失敗。有人知道什么失敗了嗎?
發(fā)送請求后 Firebase http 無法設(shè)置標(biāo)頭
Qyouu
2023-04-20 10:05:39