Why This Happen, what is wrong? Function is not working corrtectly

I have 3 notificatin for 3 trips. this code fetch notes from note tables then trip detail which are in tripdetail table (master detail)

Getnotifications() => fetch 3 notes and push to notes array. => notes finally contain 4 object
first object is null.
GetTrips() => send 4 times value (contain null value) in to Tripdetail

Getnotifications() => execute get exactly 3 times but the 2 results return.

import { Component } from '@angular/core';
import { NavController, NavParams,ModalController } from 'ionic-angular';
import { Http } from '@angular/http';
import { AlertController } from 'ionic-angular';
import { SampleModalPage } from '../sample-modal/sample-modal';

@Component({
  selector: 'page-triplist-modal',
  templateUrl: 'triplist-modal.html'
})
export class TriplistModalPage {
public trip ;
 public notes=[{ 
          "TripID" : null,
          "NoteID":null
        }];

 public trips=[{ "Token" :null ,
          "UDI" : null,
          "TripID" : null,
          "NoteID":null,
          "START_ADDRESS":null,
          "END_ADDRESS":null,
          "NAME":null,
          "PHONE":null,
          "COST":null,
          "STARTPOINT":null,
          "ENDPOINT":null}];

public UDI : string = this.navParams.get('UDI');
public Token: string = this.navParams.get('Token');

  constructor(public navCtrl: NavController, public navParams: NavParams, public http: Http, public alertCtrl: AlertController, public modalCtrl: ModalController) {  }

  ionViewDidLoad() {
   this.Getnotifications();
  }

Getnotifications(){
  let Url='http://mydomain/getmynotification/'+this.UDI+'/'+this.Token;
  console.log(Url);
  this.http.get(Url)
  .map(res => res.json())
    		.subscribe(data => {       
          if (data.notes==null){
            let noservice = this.alertCtrl.create({
              title: 'AH AH',
              subTitle: 'No Note',
              buttons: ['OK'] 
              });
          noservice.present();
        } else{
          for(let note of data.notes) {
            let newnote={
              NoteID : note.ID,
              TripID : note.TRIP
            }
        this.notes.push(newnote);
        }

        }
        this.GetTrips();
    });
}
GetTrips(){
  for (let nots of this.notes) {
    if (nots.NoteID != null){
      console.log(nots);
  this.Tripdetail(nots.NoteID,not.TripID);
    }
  }
}

Tripdetail(NoteID,TripID) {
  let Url='http://37.32.125.26:8081/ords/abber/driver/gettripdetail/'+this.UDI+'/'+this.Token+'/'+NoteID;
  console.log(Url);
  this.http.get(Url)
  .map(res => res.json())
    		.subscribe(data => {
          console.log(data);
          if(NoteID != null) {
    let add= {
          Token : this.Token ,
          UDI : this.UDI,
          TripID : TripID,
          START_ADDRESS: data.trip[0].START_ADDRESS,
          END_ADDRESS: data.trip[0].END_ADDRESS,
          NAME: data.trip[0].NAME,
          PHONE: data.trip[0].PHONE,
          COST: data.trip[0].COST,
          STARTPOINT: data.trip[0].STARTPOINT,
          ENDPOINT: data.trip[0].ENDPOINT,
          NoteID: NoteID
      }
   
   this.trips.push(add);
  console.log(add);    
  }
  

          //this.cost=this.trip.COST;
          //console.log(add);           
          //let myModal = this.modalCtrl.create(SampleModalPage);
          //myModal.present();
        });
        console.log(this.trips);           
    }

}

Initialized:

public  notes=[{ 
      "TripID" : null,
      "NoteID":null
    }];

then after your http.get

for(let note of data.notes) {
        let newnote={
          NoteID : note.ID,
          TripID : note.TRIP
        }

So for me it makes sense that you always get an array containing a first object (containing null) and then your fetched data, respectivelly you fetch 3 notes and get an array of 4

If that’s your bug, your could initialize your array like following:

public notes = new Array();

Thank You for your help i have updated public notes and trips array to empty array as you suggested . I will add all codes with domain address to check.

The problem is for some unknown reason in second get data loses.



import { Component } from '@angular/core';
import { NavController, NavParams,ModalController } from 'ionic-angular';
import { Http } from '@angular/http';
import { AlertController } from 'ionic-angular';
import { SampleModalPage } from '../sample-modal/sample-modal';

@Component({
  selector: 'page-triplist-modal',
  templateUrl: 'triplist-modal.html'
})
export class TriplistModalPage {
public UDI ='BBQS1NMGT8EZCRG1TFAS';
public Token='BWH08D5YQP2SBGYE5MQ5';

public notes = new Array();
public trips = new Array();

  constructor(public navCtrl: NavController, public navParams: NavParams, public http: Http, public alertCtrl: AlertController, public modalCtrl: ModalController) {
  }

  ionViewDidLoad() {
   this.Getnotifications();   
  }

Getnotifications(){
  let Url='http://mydomain/getmynotification/'+this.UDI+'/'+this.Token;
  this.http.get(Url)
  .map(res => res.json())
    		.subscribe(data => {    		
          //console.log(data);
          if (data.notes==null){
            let noservice = this.alertCtrl.create({
              title: 'AH AH',
              subTitle: 'No Note',
              buttons: ['OK'] 
              });
          noservice.present();
        } else{
          for(let note of data.notes) {
            let newnote={
              NoteID : note.ID,
              TripID : note.TRIP
            }
        this.notes.push(newnote);
        }
      }
        this.GetTrips();
    });
}

GetTrips(){
  //this.notes =this.notes.slice(1);
  for (let not of this.notes) {
    if (not.NoteID != null){
      console.log(not);
  this.Tripdetail(not.NoteID,not.TripID);
    } else {
      console.log(not);
    }
  }
}

Tripdetail(NoteID,TripID) {
  let Url='http://mydomain/gettripdetail/'+this.UDI+'/'+this.Token+'/'+NoteID;
  this.http.get(Url)
  .map(res => res.json())
    		.subscribe(data => {          
          if(NoteID != null) {
          console.log(NoteID);
    let add= {
          Token : this.Token ,
          UDI : this.UDI,
          TripID : TripID,
          START_ADDRESS: data.trip[0].START_ADDRESS,
          END_ADDRESS: data.trip[0].END_ADDRESS,
          NAME: data.trip[0].NAME,
          PHONE: data.trip[0].PHONE,
          COST: data.trip[0].COST,
          STARTPOINT: data.trip[0].STARTPOINT,
          ENDPOINT: data.trip[0].ENDPOINT,
          NoteID: NoteID
      }
   this.trips.push(add);
   console.log(add);
  }
  
        });
        console.log(this.trips);           
    }

}

What’s

in second get 

? I don’t undertand, which action/piece of code are you speaking and how is it triggered?

if you run this code you will find that something wrong happens on Tripdetail(NoteID,TripID) however this also is ok because in log I see gettripdetail is fine and 3 times fired correctly but 2 times add array pushed into trips and 3rd time it fails,

When you debug, did you land 3 times in the http.get of your Tripdetail method respectively is this http.get successfull 3 times no error?

Try to debug, there is maybe something weird because http.get gonna be executed in promises? Not sure, but since it will be executed in different “threads”, that has this weird effect on this.trips.push(add); ?


In blue boxes as you can see the counts shows the the times of Tripdetail(not.NoteID,not.TripID); calling
in Violet boxes you can see the gettripdetail web service return json but third time red box it is empty however the noteID and tipID which passed is correct but result is empty.

Why? I don’t understand.

I’ve merged GetTrips code withTripdetail but the same result.

When you call manually

let Url='http://37.32.125.26:8081/ords/abber/driver/getmynotification/'+this.UDI+'/'+this.Token;

with the “third time red box” details, do you get an empty json result?

1 Like

MY God.
This tripdetail is removed from table manually from back-end team. but its header is not removed from Notification table.

2 Likes

That back-end team :wink:

Good to know you figured out what was wrong, congrats!

Yes after 24 hours it is looks more comic. Normally if it delete by app noteID should be removed too but noteId was exist.