点赞和点赞功能期间Firebase功能出现错误500

时间:2019-06-07 18:57:54

标签: javascript angular firebase ionic-framework ionic4

我目前正在观看从udemy购买的教程,在实施过程中,我在实施“喜欢”或“不喜欢”功能时遇到问题。

向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)

用于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>
...

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)

2 个答案:

答案 0 :(得分:1)

if (typeof (request.body) === 'string') {
        body = JSON.parse(request.body);
    } else {
        body = request.body;
    }

通过添加这些代码行,问题就解决了。谢谢!

答案 1 :(得分:0)

FILE *fp;
  fp = fopen("/media/card/kernel.txt", "r");
  n=0;
  //Read from the file. 
  while(fscanf(fp, "%f", &array[n++])!= EOF);
  // Copy the whole data to memory at the given address
  memcpy(weights, array, sizeof(array) );

// Read back first few and last few values back just to cross check on functionality.
    for (int j = 0 ; j < 10; j++) {
               SysMem= *((float *) (weights+j*0x04));
     printf("Read %f\n", SysMem);
    }

               for (int j = 15874861 ; j > 15874850; j--) {
               SysMem= *((float *) (weights+j*0x04));
     printf("Read %f\n", SysMem);
    }

这是您失败的那一行,您的邮递员原始数据需要采用JSON格式。默认情况下设置为文本,必须将其更改为JSON(application / json)

还要确保您的函数返回一个诺言

if (!is.string(resourcePath) || resourcePath === '') {
  throw new Error(`Path must be a non-empty string.`);
}

我为您的函数添加了两个return语句,这些函数知道何时完成,还请确保文档使用eventId作为字符串。

最后,我不需要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); }) }) 的有效负载,就可以按原样发送对象了。