【问题标题】:Angular-firebase. Data is not inserted correctly角度火力基地。数据未正确插入
【发布时间】:2016-04-26 10:26:50
【问题描述】:

我希望我的数据像这样插入到 firebase 中:

cities:{
    Tokyo:{
        name: "Tokyo, JP"
    }
    London:{
        name: "London, UK"
    }

    and so on...

我从在线 GUI 手动插入了一些数据:cities

但不幸的是它被插入了:minsk

我的代码

Firebase 工厂:

export default function CitiesFactory($firebaseArray, Firebase) {
    'ngInject';
    var ref = new Firebase("https://name.firebaseio.com/");
    return $firebaseArray(ref.child('cities'));
}

控制器(添加功能):

    $scope.addoras = function(city) {
        CitiesFactory.$add({
            city: {
                name: city
            }
        }).then(function(CitiesFactory) {
            var id = CitiesFactory.key();
            console.log('Added Contact ' + id);
            $scope.addorasmodel = '';

        });
    };

有人可以帮我吗?

【问题讨论】:

标签: angularjs controller firebase firebase-realtime-database


【解决方案1】:

当您使用 $add() 时,它会生成一个唯一的推送 ID,正如它在 documentation 中所说的那样。

如果你想避免这些唯一的 id,你可以使用 set() 或 update() (Documentation)

var ref = new Firebase("https://name.firebaseio.com/");
ref.set({
    city: {
        name: city
    }
});

var ref = new Firebase("https://name.firebaseio.com/");
ref.update({
    city: {
        name: city
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-17
    • 1970-01-01
    • 2019-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    相关资源
    最近更新 更多