所以我正在学习firebase功能,而且我试图让一个功能检测到DB的变化,但它并没有。它假设在游戏模式改变时被检测到虽然它没有做什么。如果它确实检测到了更改,它会将其更改为游戏模式3,尽管它声明它没有做任何事情。这是通过firestore
完成的我的测试数据库:https://gyazo.com/91afd83cd27a0e7c55bd79b2b86529bf
以下是我触发它的方法: https://gyazo.com/8c7206d80a343b0e7ee9432cf3fae47c
和我的node.js脚本如下:
exports.tellGameModeofUser = functions.firestore
.document('users/{userId}')
.onUpdate(event => {
// Retrieve the current and previous value
const data = event.data.data();
const previousData = event.data.previous.data();
// We'll only update if the name has changed.
// This is crucial to prevent infinite loops.
console.log("the new game mode: " + data );
console.log("old gmae mode: " + previousData)
if (data.gamemode === previousData.gamemode){
return;
}else if (data.gamemode === "1"){
console.log("value changed game mode on");
}
});
当我检查日志时看不到任何帖子,没有触发器。
答案 0 :(得分:1)
云功能已更新,因此您需要更改为以下内容:
exports.tellGameModeofUser = functions.firestore
.document('users/{userId}')
.onUpdate(event => {
const data = event.data.data();
const previousData = event.data.previous.data();
到此:
exports.tellGameModeofUser = functions.firestore.document('users/{userId}').onUpdate((change,context) => {
const data = change.after.data();
const previousData = change.before.data();
});
更多信息:
https://blog.cloudboost.io/getting-started-with-react-native-and-redux-6cd4addeb29
答案 1 :(得分:0)
所以1我必须将firebase更新到1.0.0并且允许我使用新的函数语法。然后我能够使用onUpdate函数的新语法,并根据需要运行该函数。
exports.tellGameModeofUser = functions.firestore.document('Users/{userId}')
.onUpdate((change,context) => {
console.log("Hey");
console.log("change: " +change);
const beforeData = change.before.data() // data before the write
const afterData = change.after.data(); // data after the write
// We'll only update if the name has changed.
// This is crucial to prevent infinite loops.
console.log("the new game mode: " + afterData );
console.log("old gmae mode: " + beforeData)
if (afterData.gamemode === beforeData.gamemode){
console.log("game mode is the same");
return;
}else if (afterData.gamemode === "1"){
console.log("value changed game mode on");
}
});