我有一个用 ionic 和 firebase 构建的聊天应用程序。我有一个简单的加入 channel 函数,它获取 channel ID,并将当前用户 ID 添加到数据库中的成员列表中。根据我所看到的一切,它工作正常,但是每当我进入 firebase 控制台时,它都不会在数据库中显示新项目。这是当用户加入聊天 channel 时我用来将项目添加到数据库的代码...
joinChannel(type, user_id) {
return new Promise((resolve, reject) => {
if(this.availableChannels[type] !== undefined) {
console.log("joining channel: " + type);
console.log('for user: ' + user_id);
let update = {};
update['/chat/members/' + type + '/' + user_id] = {active: true, joinedAt: firebase.database.ServerValue.TIMESTAMP};
this.afDB.database.ref().update(update).then(() => {
console.log("updated /chat/members/" + type + "/" + user_id + " to");
console.log({active: true, joinedAt: firebase.database.ServerValue.TIMESTAMP});
this.activeChannel = '/channels/' + type;
resolve(true);
})
} else {
reject("channel_not_found");
}
});
}
现在,如果我在 Chrome 中打开开发者控制台并单击加入 channel ,我会在控制台中看到此信息...
joining channel: news
chat.ts:210 for user: [USERIDHERE]
chat.ts:214 updated /chat/members/news/[USERIDHERE] to
chat.ts:215 Object
active: true
joinedAt: Object
.sv: "timestamp"
__proto__: Object
__proto__: Object
因此控制台显示数据库运行了更新,但是如果我随后登录到我的 Firebase 控制台,“聊天/成员”数据库中没有“新闻”项目。我已经用不同的 channel 尝试过,并且总是从 firebase 得到相同的确切响应,说它更新了数据库,但在 firebase 控制台中该项目仍然为空。
以防万一需要 this.afDB
变量是
公共(public) afDB:AngularFireDatabase
在构造函数中。我知道数据库正在工作,因为我可以更新内容,在聊天 channel 中发送消息,并且它们出现在 firebase 控制台中,但无论出于何种原因,当用户加入 channel 时,我都会获得成功的控制台日志,但数据库没有更新随着新的信息。我对这个问题真的很迷茫,确实需要一些帮助。
最佳答案
在我看来你想使用一组而不是更新
this.afDB.database.ref('/chat/members/' + type + '/' + user_id)
.set({active: true, joinedAt: firebase.database.ServerValue.TIMESTAMP});
关于javascript - 即使响应成功,firebase 数据库也不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45926562/