Nox
Nox

Reputation: 1

Angular 4 - http.post doesn't receive list of objects

I would like to make a http.post request, to receive a list of objects from PHP with .subscribe.

When I do that, I receive something like a string which contains my objects. But in a *ngFor loop, the console says me the list is an undefined variable. Here is my code :

list-master.ts

  /////
  objet01 = {ville: 'Bordeaux', prix: '69€'};
  objet02 = {ville: 'Lille', prix: '45€'};
  objs = [this.objet01, this.objet02];
  /////

  tempproj;


  constructor(public navCtrl: NavController, private navParams: NavParams, public http: Http) {

    this.http = http;

    this.getprojets();

  }

  getprojets() {
    var link = 'http://*****/getprojets.php';
    var myData = JSON.stringify({value: 'true'});

    this.http.post(link, myData)
    .subscribe(data => {
    this.tempproj = data["_body"]; 
    console.log('OKOK ' + this.tempproj + ' OKOK');
    }, error => {
    console.log("Oooops!");
    });

  }

Here is getprojets.php:

$postdata = file_get_contents("php://input");
    if (isset($postdata)) {
        $request = json_decode($postdata);
        $value = $request->value;

        if ($value === 'true' ) {
            $bdd = new PDO('***', '***', '***', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
            $bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);

            $query = $bdd->prepare("SELECT * FROM projetsapparts") or die('Login invalide');
            $query->execute();
            $liste = array();
            class projet1 {};


 while ($projet = $query->fetch()) {
            $projet1 = new projet1;
            $projet1->ville = $projet['ville'];
            $projet1->photo = $projet['photo'];
            $liste[] = $projet1;
        }
        echo json_encode($liste);
    }
}
else {
    echo 'Erreur postdata';
}

And then, the list-master.html:

  <ion-slides class="marketslide" autoplay="3000" loop="true">
    <ion-slide *ngFor="let appart of tempproj">
      <ion-card (tap)="showplvdetails()">
        <img src="{{appart.photo}}">
        <div class="card-title">{{appart.ville}}</div>
      </ion-card>
    </ion-slide>
  </ion-slides> 

In the console, the var tempproj contains 3 entries, it looks like a list of objects : [{"ville":"Reims","photo":"http:******.jpg"},{"ville":"Paris","photo":"http:******.jpg"},{"ville":"Amsterdam","photo":"http:******.jpg"}]

But, I have this error:

Cannot read property 'hasAttribute' of undefined

When in the .html file, I replace the var tempproj by the var objs (which is set in .ts file instead of coming from PHP) in the *ngFor, all is working fine.

Why does the list of objects from PHP be undefined? On the *ngFor="let appart of tempproj" line, does the 'appart' can be an object? Please help me, I only find solutions for JavaScript and Angular 2. I'm on it for 5 days.

Upvotes: 0

Views: 846

Answers (1)

TuringCreep
TuringCreep

Reputation: 59

The problem here is your tempproj object is undefined at the time your template renders.

Perhaps you could try initializing it to an empty array,

let tempproj: Array<any> = [];

constructor(public navCtrl: NavController, private navParams: NavParams, public http: Http) {

    ....

Upvotes: 1

Related Questions