0

I want to push new item to my firebase database.

Json object should look like this:

var newItem = {
            'address': "Кабанбай батыр, 53",
            'cityId': 1,
            'courierName': "Максат",
            'dispatcherId': "somedispatcherId",
            'info': "привезти к 2ому подъезу в 11 ч вечера", 
            'isCompleted': 0,
            'isProceeded': 0, 
            'name': "Адиль Жалелов" ,
            'paymentMethod': "наличка" ,
            'phone': "87775634456" ,
            'price': 5000,
            'products': []
        }

And the problem is that i cant push array of objects($scope.orders) to newItem

i tried to push this $scope.orders:

   $scope.addOrder = function(){
        var orderRef = 
        firebase.database().ref().child('Orders').child('Astana');
        var newOrderKey = orderRef.push().key;
        var newJson = {
            'address': "Кабанбай батыр, 53",
            'cityId': 1,
            'courierName': "Максат",
            'dispatcherId': "somedispatcherId",
            'info': "привезти к 2ому подъезу в 11 ч вечера", 
            'isCompleted': 0,
            'isProceeded': 0, 
            'name': "Адиль Жалелов" ,
            'paymentMethod': "наличка" ,
            'phone': "87775634456" ,
            'price': 5000,
            'products': []
        };
         newJson['products'].push( $scope.orders);
        /*for (var i =0; i < $scope.orders.length ; i++){
            newJson['products'].push( $scope.orders[0]);
        } */

        console.log($scope.orders,newJson)
        orderRef.child(newOrderKey).set(newJson); 
    } 

But i got error : Reference.set failed: First argument contains an invalid key ($$hashKey) in property....

if console.log($scope.orders) then get

xcxx

Anyone who can help?

thanks in advance!

4

1 回答 1

3

在 Firebase 中,Push方法使用唯一键生成新的子位置并返回其参考。所以你已经有了孩子和孩子的参考。您不需要像您尝试的那样将孩子添加到您的 orderRef 中。

你可以简单地做到这一点

var newOrderKey = orderRef.push();  // Return the ref of the futur child
newOrderKey.set(newJson); 

来自角度的对象也存在问题,“$$hashKey”中的“$”是问题所在。Angular 创建 $$hashKey 属性。

这应该可以解决问题

newJson['products'].push( angular.fromJson(angular.toJson($scope.orders))); 
于 2017-08-16T19:15:13.907 回答