gpt4 book ai didi

angular - Firebase 云函数以状态码 304 结束

转载 作者:行者123 更新时间:2023-12-04 14:20:43 24 4
gpt4 key购买 nike

我无法找出为什么以下函数以状态代码 304 结束而不是发送 200 响应。文档按要求更新,但我想获得正确的状态代码。

const firestoreInstance = admin.firestore();

export async function onTwitchGiveawayPostback(req, res) {
const reward = 1;

const userName = await utils.getParameterByName('name', req.originalUrl);

if (!userName) { throw new Error('User Display Name is not set on REQLoot :('); }

const displayNameDocPath = firestoreInstance.collection('displayNames').doc(userName);

return displayNameDocPath.get().then(snapshot => snapshot.data()).then(displayNameDocDetails => {
if (!displayNameDocDetails) {
res.status(200).send('User Display Name: ' + userName + ' is not set on REQLoot :(');
return Promise.resolve('Ok');
} else {
const uid = displayNameDocDetails.uid;

const staminaEntryDoc = firestoreInstance.collection(utils.API_URLS.userStaminaBalanceSheet + '/' + uid + '/entries').doc();
const userDocPath = firestoreInstance.collection('users').doc(uid);

const promises = [];
promises.push(
userDocPath.get(),
);

return Promise.all(promises).then((snapshots) => {
return Promise.all(snapshots.map((doc) => {
return doc.data();
})).then(data => {
const userDocDetails = data[0];

const serverTimestamp = admin.firestore.Timestamp.now();

const batch = firestoreInstance.batch();

const staminaBalanceSheetDescription = 'winner';
const staminaPrev = userDocDetails.stamina;
const staminaNew = staminaPrev + reward;

batch.update(userDocPath, {
stamina: staminaNew,
updatedAt: serverTimestamp,
});

// Update the User's stamina balance sheet
batch.set(staminaEntryDoc, {
description: staminaBalanceSheetDescription,
stamina: {
previous: staminaPrev,
new: staminaNew,
},
staminaAmount: reward,
updatedAt: serverTimestamp,
});

return batch.commit();
})
.then(() => {
res.status(200).send(userName + ' won some stamina!');
})
.catch((err) => {
console.error(err)
return err;
});
})
}
})
.catch(err => console.log(err))
}

其他帖子提到函数总是需要返回一个 Promise,但我想我已经在必要的地方添加了它们。我缺少什么阻止了正常的 200 响应?

最佳答案

问题是 CORS。

该函数正在执行和更新 firestore,但返回 304 状态代码并且未发送字符串负载。使用以下行更新函数:

res.set('Access-Control-Allow-Origin', '*');

允许在完成时发送 200 状态代码和正确的字符串。

关于angular - Firebase 云函数以状态码 304 结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55328443/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com