【问题标题】:Angular pushing data to user nodeAngular 将数据推送到用户节点
【发布时间】:2018-06-24 12:52:59
【问题描述】:

我正在 Ionic 中构建我的第一个移动应用程序,一切正常 进展顺利。我创建了一个用户登录名,用户在登录后会在 firebase 中获得自己的节点。但是,我想将数据保存到 登录用户。目前数据正在保存到自己的节点。我的 方法如下

this.diary = db.list('UID');//this is where I think I am going wrong?

handler: data=> {
                    this.diary.push({
                        country: data.a,
                        city: data.b,
                        month: data.c,
                        day: data.d,
                        time: data.e
                })

任何帮助都会很棒,在此先感谢

【问题讨论】:

    标签: angular firebase firebase-realtime-database ionic2


    【解决方案1】:

    每次您在数据库引用上调用 push() 时,Firebase 都会在该引用下创建一个新的唯一位置。

    您似乎希望将数据存储在用户的 UID 下,而不是新的唯一位置下。为此,请致电 child(...) 而不是 push()。例如

    this.diary = db.list('UID');
    let user = firebase.auth().currentUser;
    
    handler: data=> {
                    this.diary.ref.child(user.uid).set({
                        country: data.a,
                        city: data.b,
                        month: data.c,
                        day: data.d,
                        time: data.e
                })
    

    但在这种情况下,如果您绕过 AngularFire 进行此操作并直接使用 JavaScript SDK,可能会更容易:

    db.child('UID').child(user.uid).set(...)
    

    【讨论】:

    • 我已经改成了这个,但是我得到了错误'child does not exist on type firebaselistobservable'
    • 嗯...确实没有。在这种情况下,它应该类似于this.diary.ref.child(...)。但是如果你直接进入数据库就更容易了:db.child('UID').child(user.uid).set(...)`。
    猜你喜欢
    • 1970-01-01
    • 2015-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多