gpt4 book ai didi

javascript - Firebase 云函数 - TypeError : Cannot read property 'data' of undefined when data exists

转载 作者:行者123 更新时间:2023-11-30 21:11:44 26 4
gpt4 key购买 nike

我有以下云功能:

exports.keepPostKeysUpdated = functions.database.ref('/posts/{postid}').onWrite(event => { 
console.log("write on posts...");
console.log(event.previous.params.postID);
console.log(event.data.previous.val());
console.log(event.data.previous.val().postID);
var postKey = event.data.previous.val().postID;


// When a table, category, or region is changed the old upload has to be deleted
if (event.data.previous.exists()) {
if (event.data.previous.val().table != event.data.val().table || event.data.previous.val().region !=
event.data.val().region || event.previous.data.val().category != event.data.val().category) {
// category, region, or table was changed
console.log(postKey);
if (event.data.previous.val().table != event.data.val().table) {
console.log("Table was changed");
// delete the post from the old table
const oldTable = event.data.previous.val().table;
const newTable = event.data.val().table;

addToNewTable(newTable, postKey);
removePostFromOldTable(oldTable, postKey);
}
if (event.data.previous.val().category != event.data.val().category) {
console.log("Category was changed");
// delete the post from the old category
const oldCategory = event.data.previous.val().category;
const newCategory = event.data.val().category;

addToNewCategory(newCategory, postKey);
removePostFromOldCategory(oldCategory, postKey);
}
if (event.data.previous.val().region != event.data.val().region) {
console.log("Region was changed");
// delete post from old region
const oldRegion = event.data.previous.val().region;
const newRegion = event.data.val().region;

addToNewRegion(newRegion, postKey);
removePostFromOldRegion(oldRegion, postKey);
}
}
else {
return
}
}
else {
// previous value does not exist this case is handled by
// copyPostKey
return
}
});

当表或区域更改时,它工作得很好,但每次更改类别时都会失败。错误来自行 var postKey = event.data.previous.val().postID; 如何有时读取此值但有时不读取?我什至可以控制台记录 key ,但它说当我尝试将其分配给 postKey 时无法读取。知道这个问题出自什么吗?

我的 iOS 应用程序始终以相同的方式写入数据

ref.child("posts").child(editedPost.postID).updateChildValues(["table": editedPost.table])
ref.child("posts").child(editedPost.postID).updateChildValues(["category": editedPost.category])
ref.child("posts").child(editedPost.postID).updateChildValues(["region": editedPost.region])

节点 = v6.11.2firebase 工具 = 3.10.10

最佳答案

previous.val() 只有在前一个值存在时才有效。如果这是该路径的第一次写入,它可能不存在。在引用它之前,您应该检查它是否存在:

event.data.previous.exists()

https://firebase.google.com/docs/database/extend-with-functions#reading_the_previous_value

关于javascript - Firebase 云函数 - TypeError : Cannot read property 'data' of undefined when data exists,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46063941/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com