【发布时间】:2020-11-18 06:49:52
【问题描述】:
我有以下实时数据库架构:
我正在开发一个用户可以喜欢另一个用户的社交应用。当用户点击like按钮时,会在myLikes->userId列表中添加一个新条目
MyLike myLike = new MyLike(userId, System.currentTimeMillis();
FirebaseDatabase.getInstance().getReference().child("myLikes").child(userId).child(System.currentTimeMillis()).setValue(myLike);
当新条目完成后,会触发云函数在 whoLikedMe>userId 列表中为其他已被赞的用户写入新条目。
exports.whoLikedMe = functions.database.ref('/myLikes/{userId}')
.onWrite((change, context) => {
// Only edit data when it is first created.
if (change.before.exists()) {
return null;
}
// Exit when the data is deleted.
if (!change.after.exists()) {
return null;
}
// Grab the current value of what was written to the Realtime Database.
const data2 = change.after.val();
const likedDate = data2.date;
const myUserId = data2.I_Liked_UserId;
var likedMe = {
date: likedDate,
Who_Liked_Me_UserId: myUserId,
}
//get the current date in millis
var hrTime = process.hrtime();
const dateMillis = hrTime[0];
return admin.database().ref('/WholikedMe/'+myUserId+'/'+hrTime[0]).set(likedMe);
});
函数触发正常,但没有条目插入实时数据库。我做错了什么?
编辑:
当我改变时:
exports.whoLikedMe = functions.database.ref('/myLikes/{userId}')
与
exports.whoLikedMe = functions.database.ref('/myLikes/{userId}/915170400000')
(将硬编码的时间戳添加到路径)一切正常。问题是时间戳是不断变化的。知道如何使用时间戳来完成此操作吗?
【问题讨论】:
标签: android firebase firebase-realtime-database google-cloud-functions