1

I am trying to get the provCode or ID from the provinceData from getFunction(). I did that by using angular.forEach(), but I didn't expected to get the following error.


Notice: Undefined property: stdClass::$provCode in C:\xampp\htdocs\student-violation-system\php\address\cities.php on line 5
[]

I tried to change the {provCode: provCode} to just provCode also not working.

So, I tried to change the {provCode: provCode} to {provCode: '0456'} and it works.

But ofcourse it is not what I want, I want it to be dynamic. I wonder why it is saying the Undefined property because I tried to console.log the provCode inside the getCity() and it has a expected value of 0456. So, I wonder why it is saying Undefined property.

app.factory

factory.getProvince = function(){
    return $http.get('php/address/province.php').then(function(response){
        provinceData = response.data;
        console.log(provinceData); 

        angular.forEach(provinceData, function(value, key) {
            if(value.provDesc == province){
                provCode =  provinceData[key].provCode;
            }   
        }); // angular.forEach

        return provinceData;
    })
}

factory.getCity = function(){
    return $http.post('php/address/cities.php', {provCode: provCode}).then(function(response){

        console.log(provCode);

        municipalityData = response.data;
        console.log(municipalityData);  
        return municipalityData;
    })
}


return factory;

cities.php

<?php 
include("../../connection.php");

$data = json_decode(file_get_contents("php://input"));
$provCode = $data->provCode;

$db->query('SET CHARACTER SET utf8');
$cities = $db->query(" SELECT * FROM refcitymun WHERE provCode='$provCode' ");

$cities = $cities->fetchAll();

echo json_encode($cities);
?>

UPDATE

I log the $data in cities.php and it was returning null value. I don't know why because I console.log the provCode inside the getCity function and it is returning the right value/data I was expecting. Can someone explain it to me what is happening? My mind is going crazy on this one.

By the way it is one of my ongoing approach on how to solve my issue on this: Anglularjs passing value from Controller, State Resolve and Factory Service

Marksmanship
  • 169
  • 1
  • 11

1 Answers1

0

It seems you're defining provCode as a global variable. Try declaring it as a private variable of the factory first.

Example

function MyFactory($http) {
  let provCode = 0;
  return {
    getProvince: function () {
      return $http.get('php/address/province.php')
        .then(function(response){
          // do some stuff
          angular.forEach(provinceData, function(value, key) {
            if(value.provDesc == province){
              provCode =  provinceData[key].provCode;
            }   
          }); // angular.forEach
        })
    },
    getCity: function() {
      return $http.post('php/address/cities.php', {provCode: provCode})
        .then(function(response){
          // do some stuff
        })
    }
  };
}
angular
  .module('app')
  .factory('MyFactory', MyFactory);
Baruch
  • 2,381
  • 1
  • 17
  • 29
  • `provCode` is already declared inside my `app.factory` `app.factory('dataservice', function($stateParams, $http){ var factory = {}; var provCode; }` – Marksmanship Jun 16 '17 at 05:21