Javascript Like&;期间Firebase函数出现错误500;厌恶特征
我目前正在观看从udemy购买的教程,在实现过程中,我在实现Like或unlike特性时遇到了一个问题 当我向Firebase函数发出post请求时,控制台中出现错误500,在Firebase函数日志中我看到此错误:Javascript Like&;期间Firebase函数出现错误500;厌恶特征,javascript,angular,firebase,ionic-framework,ionic4,Javascript,Angular,Firebase,Ionic Framework,Ionic4,我目前正在观看从udemy购买的教程,在实现过程中,我在实现Like或unlike特性时遇到了一个问题 当我向Firebase函数发出post请求时,控制台中出现错误500,在Firebase函数日志中我看到此错误: Value for argument "documentPath" is not a valid resource path. Path must be a non-empty string. at Object.validateResourcePath (/srv/nod
Value for argument "documentPath" is not a valid resource path. Path must be a non-empty string.
at Object.validateResourcePath (/srv/node_modules/@google-cloud/firestore/build/src/path.js:403:15)
at CollectionReference.doc (/srv/node_modules/@google-cloud/firestore/build/src/reference.js:1718:20)
at exports.updateLikesCount.functions.https.onRequest (/srv/lib/index.js:24:44)
at cloudFunction (/srv/node_modules/firebase-functions/lib/providers/https.js:57:9)
at /worker/worker.js:783:7
at /worker/worker.js:766:11
at _combinedTickCallback (internal/process/next_tick.js:132:7)
at process._tickDomainCallback (internal/process/next_tick.js:219:9)
Value for argument "documentPath" is not a valid resource path. Path must be a non-empty string.
at Object.validateResourcePath (/srv/node_modules/@google-cloud/firestore/build/src/path.js:403:15)
at CollectionReference.doc (/srv/node_modules/@google-cloud/firestore/build/src/reference.js:1718:20)
at exports.updateLikesCount.functions.https.onRequest (/srv/lib/index.js:24:44)
at cloudFunction (/srv/node_modules/firebase-functions/lib/providers/https.js:57:9)
at /worker/worker.js:783:7
at /worker/worker.js:766:11
at _combinedTickCallback (internal/process/next_tick.js:132:7)
at process._tickDomainCallback (internal/process/next_tick.js:219:9)
firebase的Index.js文件函数
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp(functions.config().firebase);
// // Start writing Firebase Functions
// // https://firebase.google.com/docs/functions/typescript
export const updateLikesCount = functions.https.onRequest((request, response) => {
console.log(request.body);
const eventId = request.body.eventId;
const userId = request.body.userId;
const state = request.body.state; //like or unlike
// tslint:disable-next-line: no-floating-promises
admin.firestore().collection("events").doc(eventId).get().then((data: any) => {
let likesCount = data.data().likesCount || 0;
let likes = data.data().likes || [];
let updateData = {} as any;
if (state === "like") {
updateData["likesCount"] = ++likesCount;
updateData[`likes.${userId}`] = true;
}
else {
updateData["likesCount"] = --likesCount;
updateData[`likes.${userId}`] = false;
}
admin.firestore().collection("events").doc(eventId).update(updateData).then(() => {
response.status(200).send("Done");
}).catch((err) => {
response.status(err.code).send(err.message);
})
}).catch((err) => {
response.status(err.code).send(err.message);
})
})
Feed.html
<div class = "content" *ngFor="let event of events">
<ion-button (click)="like(event)">Like</ion-button>
...
当我单击“喜欢”按钮时,我得到错误500,当我与邮递员一起测试时,我得到以下错误:
Value for argument "documentPath" is not a valid resource path. Path must be a non-empty string.
at Object.validateResourcePath (/srv/node_modules/@google-cloud/firestore/build/src/path.js:403:15)
at CollectionReference.doc (/srv/node_modules/@google-cloud/firestore/build/src/reference.js:1718:20)
at exports.updateLikesCount.functions.https.onRequest (/srv/lib/index.js:24:44)
at cloudFunction (/srv/node_modules/firebase-functions/lib/providers/https.js:57:9)
at /worker/worker.js:783:7
at /worker/worker.js:766:11
at _combinedTickCallback (internal/process/next_tick.js:132:7)
at process._tickDomainCallback (internal/process/next_tick.js:219:9)
Value for argument "documentPath" is not a valid resource path. Path must be a non-empty string.
at Object.validateResourcePath (/srv/node_modules/@google-cloud/firestore/build/src/path.js:403:15)
at CollectionReference.doc (/srv/node_modules/@google-cloud/firestore/build/src/reference.js:1718:20)
at exports.updateLikesCount.functions.https.onRequest (/srv/lib/index.js:24:44)
at cloudFunction (/srv/node_modules/firebase-functions/lib/providers/https.js:57:9)
at /worker/worker.js:783:7
at /worker/worker.js:766:11
at _combinedTickCallback (internal/process/next_tick.js:132:7)
at process._tickDomainCallback (internal/process/next_tick.js:219:9)
这是你失败的一行,你的邮递员原始数据需要是JSON格式。默认设置为text,必须将其更改为JSON(application/JSON)
另外,确保函数返回一个承诺
export const updateLikesCount = functions.https.onRequest((request, response) => {
console.log(request.body);
const eventId = request.body.eventId;
const userId = request.body.userId;
const state = request.body.state; //like or unlike
return admin.firestore().collection("events").doc(eventId).get().then((data: any) => {
let likesCount = data.data().likesCount || 0;
let likes = data.data().likes || [];
let updateData = {} as any;
if (state === "like") {
updateData["likesCount"] = ++likesCount;
updateData[`likes.${userId}`] = true;
}
else {
updateData["likesCount"] = --likesCount;
updateData[`likes.${userId}`] = false;
}
return admin.firestore().collection("events").doc(`${eventId}`).update(updateData).then(() => {
response.status(200).send("Done");
}).catch((err) => {
response.status(err.code).send(err.message);
})
}).catch((err) => {
response.status(err.code).send(err.message);
})
})
我为您的函数添加了两个返回语句,它们知道何时完成,还确保文档使用eventId作为字符串
最后,我不需要JSON.stringify(body)
您的负载,按原样发送对象;很好
if (typeof (request.body) === 'string') {
body = JSON.parse(request.body);
} else {
body = request.body;
}
通过添加这些代码行,问题就消失了。谢谢 你确定
['likes.${userId}]
不应该是['likes.${userId}]
(错误的括号)?我添加了返回值,但返回值是一样的。如果(!is.string(resourcePath)| resourcePath===''){抛出新错误(路径必须是非空字符串。
)}我不知道把这行代码放在哪里。如果我删除了JSON.Stringify,那么就会出现CORS相关的错误,请继续粉碎它