javascript - 云功能 -
问题描述
我现在使用的是 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)
知道发生了什么变化或我该如何解决?
解决方案
正如上面提到的答案,我也没有在任何地方看到 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"
]
}
推荐阅读
- python - 3D NumPy 数组到 2D 并从 3D 数组中获取两个不同的 2D 数组
- deep-learning - 如何可视化超参数和最佳分数的变化
- flutter - 如何从其他小部件访问变量
- npm - 发布 NPM 包不显示为“最新”
- java - 使用 Java for Minecraft Server 关闭 Windows 时的 MessageBox
- reactjs - 在反应中显示一个字符串
- r - 基于R中的日期列创建具有重复值的新数据框
- java - 调用具有指数回退的方法,直到列表不为空
- microsoft-graph-api - 无法从 Office 365 日历将发生事件更改为异常
- scala - 将 Chisel 测试移植到 sbt 时出错