我正在创建一个与 API 联系的 HTTP 可调用函数。但是,发回 200 时出现错误。我认为这与我在使用异步函数时犯的错误有关。这是我的代码。
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;
});
通常,代码完全按需要运行,但是当 kvk 号已经在集合中时,它需要将文档 ID 发送回 200。我不确定,但我认为这是因为它在这个代码之前发送了另一个代码,但我不明白为什么会失败。有人知道什么失败了吗?
隔江千里
相关分类