我无法找出为什么以下功能以状态码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响应?
答案 0 :(得分:0)
问题是CORS。
该函数正在执行和更新Firestore,但返回304状态代码且未发送字符串有效负载。用以下行更新功能:
res.set('Access-Control-Allow-Origin', '*');
允许输入200状态代码,并在完成时发送正确的字符串。