首页 > 解决方案 > 云功能 -

问题描述

我现在使用的是 Node.js 8 和 Firebase 云功能 2.0.0 版。

我现在对这个新版本有问题,因为有一些重大变化。

我的问题是我写了一个方法,当添加一个新的孩子时,它会在 Firebase 实时数据库中监听一个节点。此方法被触发,然后将数据发送到数据库中的另一个节点。

代码

exports.addlikechange = functions.database.ref('/likes/{postid}/{userUID}').onWrite((change,context) => {
    const model = change.after.val();

    let genre = model.genre;
    let videoID = model.videoID;
    let userVideoID = model.userVideoID;

    console.log("Start func","--------> Start Like Count <----- ");
    console.log("model: ",model);
    console.log("genre: ",genre);
    console.log("videoId: ",videoID);
    console.log("userVideoID: ",userVideoID);

    const countRef = collectionRef.child('likes');


    // Return the promise from countRef.transaction() so our function 
    // waits for this async event to complete before it exits.
     return countRef.transaction(current => {
      if (change.data.exists() && !change.data.previous.exists()) {
        const genreList = admin.database().ref(`${genre}/${videoID}/likes`).transaction(current => {
          return (current || 0) + 1;
        });
        const userList = admin.database().ref(`users/${userVideoID}/likes`).transaction(current => {
          return (current || 0) + 1;
        });
        const videoList = admin.database().ref(`videos/${userVideoID}/${videoID}/likes`).transaction(current => {
          return (current || 0) + 1;
        });
      }
     }).then(() => {
       console.log('Counter updated.');
       console.log("End func","--------> End Like Count <----- ");
     });
   });

问题在于collectionRef,在 logcat 中它说 collectionRef 没有定义。但是正如你所看到的,它是被定义的。

完全错误

ReferenceError: collectionRef is not defined
    at exports.addlikechange.functions.database.ref.onWrite (/user_code/index.js:60:36)
    at cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:120:23)
    at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:151:20)
    at /var/tmp/worker/worker.js:827:24
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)

知道发生了什么变化或我该如何解决?

标签: javascriptfirebasefirebase-realtime-databasegoogle-cloud-functions

解决方案


正如上面提到的答案,我也没有在任何地方看到 collectionRef 定义。

但我认为破坏你的(如果它以前工作过,尽管未定义 collectionRef)代码是,安装节点 8 将自动在你的tsconfig.json.

但是您也可以这样做来禁用任何未定义变量的强制执行:

像这样添加"strict": false,到您的tsconfig.json文件中:

 {
    "compilerOptions": {
    "module": "commonjs",
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "outDir": "lib",
    "sourceMap": true,
    "strict": false,
    "target": "es2015"
  },
  "compileOnSave": true,
  "include": [
    "src"
  ]
}

推荐阅读