只有在快照中某些字段发生更改(在这种情况下为“练习”)的情况下,我才具有增加计数器的云功能。
在我的云功能中,由于某种原因,我始终会触发此检查:
const before = snapshot.before.data();
const after = snapshot.after.data();
if (before['exercises'] !== after['exercises']) {
console.log(before['exercises']);
console.log(after['exercises']);
// Increment the counter...
}
与日志语句相同:
[ { exerciseId: '-LZ7UD7VR7ydveVxqzjb',
title: 'Barbell Bench Press' } ] // List of exercise objects
[ { exerciseId: '-LZ7UD7VR7ydveVxqzjb',
title: 'Barbell Bench Press' } ] // Same list of exercise objects
如何确保快照中的这些值相等?
谢谢。
答案 0 :(得分:2)
在Javascript中,对象是引用类型。如果您这样做:
{a: 1} === {a: 1}
这将是错误的,因为Javascript正在读取:
ObjectReference1 === ObjectReference2
您可以为determine the equality of two Javascript Objects做一些事情,但是如果您的对象很小,我只会做JSON.stringify
相等
const before = {
exerciseId: '-LZ7UD7VR7ydveVxqzjb',
title: 'Barbell Bench Press' }
const after = {
exerciseId: '-LZ7UD7VR7ydveVxqzjb',
title: 'Barbell Bench Press'
};
function areEqual(object1, object2) {
return JSON.stringify(object1) === JSON.stringify(object2);
}
console.log(areEqual(before, after)); /// true