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!

2
  • The error you have isn't about the push method. It is about the set. Can you add the code where you have the orderRef and newOrderKey please ? It looks like newOrderKey is invalid Commented Aug 16, 2017 at 18:55
  • @GabrielDiez check out) I think everything is ok with orderRef and OrderKey, because when i set json object witout products property it works fine Commented Aug 16, 2017 at 19:00

1 Answer 1

3

In Firebase, Push method generates a new child location using a unique key and returns its Reference. So you already have the child and the reference of your child. You don't need to add a child to your orderRef like you tried.

You can simply do that

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

Also there is an issue with object from angular, the '$' in '$$hashKey' is the issue. Angular creates the $$hashKey properties.

This should fix the issue

newJson['products'].push( angular.fromJson(angular.toJson($scope.orders))); 
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for answering, but it also gives an error: Reference.set failed: First argument contains an invalid key ($$hashKey) in property....
try this newJson['products'].push( angular.fromJson(angular.toJson($scope.orders))); When you are trying to push order from the scope
Oh thanks man! angular.fromJson(angular.toJson($scope.orders)) did the trick

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.