Firebase Cloud Functions - TypeError: Cannot read property 'data' of undefined when data exists
Firebase Cloud Functions - TypeError: Cannot read property 'data' of undefined when data exists
我有以下云功能:
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
}
});
当 table 或区域更改时它工作得很好,但每次更改类别时都会失败。错误来自行 var postKey = event.data.previous.val().postID;
如何有时读取此值但有时不能读取?我什至可以控制台记录密钥,但它说当我尝试将其分配给 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])
node = v6.11.2
firebase-工具 = 3.10.10
previous.val() 仅在前一个值存在时才有效。如果这是该路径的第一次写入,它可能不存在。您应该在引用它之前检查它是否存在:
event.data.previous.exists()
https://firebase.google.com/docs/database/extend-with-functions#reading_the_previous_value
除了Matts的回答,你还需要调整一下自己的逻辑。
exports.keepPostKeysUpdated = functions.database.ref('/posts/{postid}').onWrite(event => {
// When a table, category, or region is changed the old upload has to be deleted
if (event.data.previous.exists()) {
var postKey = event.data.previous.val().postID;
如果您在 event.data.previous.exists()
语句之后检查 postKey
,您应该没有任何问题。
可能值得看看 Firebase Documentation,它们根据写入操作有不同的触发器;
onWrite()
,在实时数据库中创建、销毁或更改数据时触发。
onCreate()
,在实时数据库中创建新数据时触发。
onUpdate()
,当实时数据库中的数据更新时触发。
onDelete()
,当数据从实时数据库中删除时触发。
对于您的情况,您可以更改逻辑以使用 onUpdate
exports.keepPostKeysUpdated = functions.database.ref('/posts/{postid}').onUpdate(event => {
var currentValue = currentValue;
var previousValue = previousValue;
var postKey = previousValue.postID;
// When a table, category, or region is changed the old upload has to be deleted
if (previousValue.table != currentValue.table || previousValue.region != currentValue.region || event.previous.data.val().category != currentValue.category) {
// category, region, or table was changed
console.log(postKey);
if (previousValue.table != currentValue.table) {
console.log("Table was changed");
// delete the post from the old table
const oldTable = previousValue.table;
const newTable = currentValue.table;
addToNewTable(newTable, postKey);
removePostFromOldTable(oldTable, postKey);
}
if (previousValue.category != currentValue.category) {
console.log("Category was changed");
// delete the post from the old category
const oldCategory = previousValue.category;
const newCategory = currentValue.category;
addToNewCategory(newCategory, postKey);
removePostFromOldCategory(oldCategory, postKey);
}
if (previousValue.region != currentValue.region) {
console.log("Region was changed");
// delete post from old region
const oldRegion = previousValue.region;
const newRegion = currentValue.region;
addToNewRegion(newRegion, postKey);
removePostFromOldRegion(oldRegion, postKey);
}
} else {
return
}
});
我有以下云功能:
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
}
});
当 table 或区域更改时它工作得很好,但每次更改类别时都会失败。错误来自行 var postKey = event.data.previous.val().postID;
如何有时读取此值但有时不能读取?我什至可以控制台记录密钥,但它说当我尝试将其分配给 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])
node = v6.11.2 firebase-工具 = 3.10.10
previous.val() 仅在前一个值存在时才有效。如果这是该路径的第一次写入,它可能不存在。您应该在引用它之前检查它是否存在:
event.data.previous.exists()
https://firebase.google.com/docs/database/extend-with-functions#reading_the_previous_value
除了Matts的回答,你还需要调整一下自己的逻辑。
exports.keepPostKeysUpdated = functions.database.ref('/posts/{postid}').onWrite(event => {
// When a table, category, or region is changed the old upload has to be deleted
if (event.data.previous.exists()) {
var postKey = event.data.previous.val().postID;
如果您在 event.data.previous.exists()
语句之后检查 postKey
,您应该没有任何问题。
可能值得看看 Firebase Documentation,它们根据写入操作有不同的触发器;
onWrite()
,在实时数据库中创建、销毁或更改数据时触发。
onCreate()
,在实时数据库中创建新数据时触发。
onUpdate()
,当实时数据库中的数据更新时触发。
onDelete()
,当数据从实时数据库中删除时触发。
对于您的情况,您可以更改逻辑以使用 onUpdate
exports.keepPostKeysUpdated = functions.database.ref('/posts/{postid}').onUpdate(event => {
var currentValue = currentValue;
var previousValue = previousValue;
var postKey = previousValue.postID;
// When a table, category, or region is changed the old upload has to be deleted
if (previousValue.table != currentValue.table || previousValue.region != currentValue.region || event.previous.data.val().category != currentValue.category) {
// category, region, or table was changed
console.log(postKey);
if (previousValue.table != currentValue.table) {
console.log("Table was changed");
// delete the post from the old table
const oldTable = previousValue.table;
const newTable = currentValue.table;
addToNewTable(newTable, postKey);
removePostFromOldTable(oldTable, postKey);
}
if (previousValue.category != currentValue.category) {
console.log("Category was changed");
// delete the post from the old category
const oldCategory = previousValue.category;
const newCategory = currentValue.category;
addToNewCategory(newCategory, postKey);
removePostFromOldCategory(oldCategory, postKey);
}
if (previousValue.region != currentValue.region) {
console.log("Region was changed");
// delete post from old region
const oldRegion = previousValue.region;
const newRegion = currentValue.region;
addToNewRegion(newRegion, postKey);
removePostFromOldRegion(oldRegion, postKey);
}
} else {
return
}
});