В настоящее время я смотрю учебник, купленный у udemy, и во время реализации я столкнулся с проблемой при реализации функции Like или Dislike.
Когда я делаю пост-запрос к функции Firebase, я получаю ошибку 500 в консоли, и в журналах функций Firebase я вижу эту ошибку:
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)
Файловые функции Index.js для firebase
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>
...
Feed.page.ts
like(event) {
let body = {
eventId: event.id,
userId: firebase.auth().currentUser.uid,
state: event.data().likes && event.data().likes[firebase.auth().currentUser.uid] == true ? "unlike" : "like",
}
// tslint:disable-next-line: max-line-length
this.http.post("https://us-central1-cp-eventoo.cloudfunctions.net/updateLikesCount", JSON.stringify(body), { responseType: "text" }).subscribe((data) => { //third parameter represents the response(200) in functions
console.log(data)
}, (error) => {
console.error(error.status);
})
}
Когда я нажимаю кнопку «Мне нравится», я получаю ошибку 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)