Search code examples
angularjsfirebaseangularjs-scopefirebase-realtime-database

Push form data to firebase not working


Hi i am having problem with push data to firebase from angular. I am newbie in this so i need help to check what is wrong with my code

this is my code: addrestaurant.html

<h4>Nama Restaurant</h4>
  <input type="text" class="form-control" ng-model="namaResto" >
  <h4>Alamat:</h4>
  <input type="text" class="form-control" ng-model="alamatResto">
  <h4>Image Link: </h4>
  <input type="text" class="form-control" ng-model="imgResto">
  <h4> Category Restoran </h4>
  <input type="text" class="form-control" ng-model="categoryResto">
  <h4>Price range:</h4>
  <input type="text" class="form-control" ng-model="pricerangeResto">
  <h4>Location:</h4>
  <input type="text" class="form-control" ng-model="locationResto">

  <span class="input-group-btn">
      <button class="btn btn-default" type="button" ng-click="sendRestaurant()">Send</button>
    </span>

main.js

/*global Firebase*/
'use strict';

angular.module('firebaseApp')
  .controller('MainCtrl', function ($scope, $timeout) {
    var rootRef = new Firebase('https://yummy-364b1.firebaseio.com/');
    var childRest = rootRef.child('restaurant');
    var categoryRest =rootRef.child('category');

    $scope.namaResto = null;
    $scope.alamatResto = null;
    $scope.categoryResto =  null;
    $scope.imgResto = null;
    $scope.pricerangeResto = null;
    $scope.locationResto = null;



   childRest.on('value', function(snapshot){
      $timeout(function(){

        var snapshotVal = snapshot.val();
        $scope.restaurant =snapshotVal;
      });
    });

 $scope.sendRestaurant = function() {


      childRest.push().set({
        nama: $scope.namaResto,
        alamat: $scope.alamatResto,
        category: $scope.categoryResto,
        img: $scope.imgResto,
        pricerange: $scope.pricerangeResto,
        locationarea: $scope.locationResto
      });
    };


 });

somehow when i remove push() and only using set() my existing data in firebase deleted. but if i using push() it doesn't change anything in firebase

sorry for my bad english. English isn't my primary language thanks for help

UPDATE I already found my problem when i declare my variable in mainCtrl

$scope.namaResto = null;
 $scope.alamatResto = null;
 $scope.categoryResto =  null;
 $scope.imgResto = null;
 $scope.pricerangeResto = null;
 $scope.locationResto = null;

these things and values that send to firebase not from input form (addrestaurant.html) So now the problem is how that my controller can send value from my input form to firebase?

SOLVED finally i found solution in this link https://stackoverflow.com/a/26905500/3628867


Solution

  • Try this:

    $scope.sendRestaurant = function() {
     childRest.push({
        nama: $scope.namaResto,
        alamat: $scope.alamatResto,
        category: $scope.categoryResto,
        img: $scope.imgResto,
        pricerange: $scope.pricerangeResto,
        locationarea: $scope.locationResto
      });
    };
    

    source: https://www.firebase.com/docs/web/guide/saving-data.html