IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Angular Discussion :

Construction fonction promise


Sujet :

Angular

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Femme Profil pro
    Développeur Web
    Inscrit en
    Mars 2021
    Messages
    82
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Mars 2021
    Messages : 82
    Par défaut Construction fonction promise
    Bonjour,

    j'ai un soucis au niveau de la construction de ma fonction promise. Je récupère uniquement la première valuer modelCar et impossible de récupérer les autres.

    J'ai bien tester mon fichier back dans postman et celui-ci marche sans problème.
    Voici le résultat de POSTMAN

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    {
        "idCar": null,
        "idCustomer": null,
        "licensePlateCar": "DS486PP",
        "brandCar": "AUDI",
        "modelCar": "A1",
        "dateOfCirculationCar": "23/06/2015",
        "motorizationCar": "90",
        "colorCar": "NOIR",
        "versionCar": "1.4 TDI",
        "urlGrayCard": null
    }
    J'aimerai donc récupérer les six valeurs qui sont non null.

    Voici le code de mon service

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
     
     
      addSIV(siv: Siv): Promise<any> {
        let promise = new Promise((resolve, reject) =>  {
          return this.httpClient.post(`${this.api}/collectCarData.php`, siv).pipe(map(
            (reponse: any) => {
              // this.modelCar = reponse.modelCar;
              // localStorage.setItem('modelCar', JSON.stringify(this.modelCar));
              this.brandCar = reponse.brandCar;
              localStorage.setItem('brandCar', JSON.stringify(this.brandCar));
              this.colorCar = reponse.colorCar;
              localStorage.setItem('colorCar', JSON.stringify(this.colorCar));
              this.versionCar = reponse.versionCar;
              localStorage.setItem('versionCar', JSON.stringify(this.versionCar));
              this.dateOfCirculationCar = reponse.dateOfCirculationCar;
              localStorage.setItem('dateOfCirculationCar', JSON.stringify(this.dateOfCirculationCar));
              this.motorizationCar = reponse.motorizationCar;
              localStorage.setItem('motorizationCar', JSON.stringify(this.motorizationCar));
            },
            resolve({modelCar: this.modelCar, brandCar: this.brandCar, colorCar: this.colorCar, versionCar: this.versionCar, dateOfCirculationCar: this.dateOfCirculationCar, motorizationCar: this.motorizationCar})
          )
          );
        });
        console.log(promise);
        return promise;
      }
    et celui de mon component

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
     
      getCar() {
        const formValue = this.bookingForm.value;
        const newSiv = new Siv(
          formValue['licensePlateCar']
        );
        return this.sivService.addSIV(newSiv).then(() => {
          this.modelCar = JSON.parse(localStorage.getItem('modelCar'));
          this.brandCar = JSON.parse(localStorage.getItem('brandCar'));
          this.dateOfCirculationCar = JSON.parse(localStorage.getItem('dateOfCirculationCar'));
          this.colorCar = JSON.parse(localStorage.getItem('colorCar'));
          this.versionCar = JSON.parse(localStorage.getItem('versionCar'));
          this.motorizationCar = JSON.parse(localStorage.getItem('motorizationCar'));
        });
      }

    Je ne comprend pas d'où vient mon erreur. Est il possible d'avoir une explication ?

  2. #2
    Membre très actif
    Homme Profil pro
    Développeur Web
    Inscrit en
    Janvier 2019
    Messages
    707
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2019
    Messages : 707
    Par défaut
    tu n'as pas besoin d'utiliser les local storages


    ça donne quoi ça ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
     getCar() {
        const formValue = this.bookingForm.value;
        const newSiv = new Siv(
          formValue['licensePlateCar']
        );
        return this.sivService.addSIV(newSiv).then((data: any) => {
    console.log(data);
    return data;
        });
      }

  3. #3
    Membre confirmé
    Femme Profil pro
    Développeur Web
    Inscrit en
    Mars 2021
    Messages
    82
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Mars 2021
    Messages : 82
    Par défaut
    Effectivement j'ai un problème car voici la réponse du console.log

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    brandCar: undefined
    colorCar: undefined
    dateOfCirculationCar: undefined
    modelCar: undefined
    motorizationCar: undefined
    versionCar: undefined
    alors je viens de faire un test au niveau du code de ma promise dans le service.

    J'ai rajoute un console.log dans la promise comme ceci

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
      addSIV(siv: Siv): Promise<any> {
        let promise = new Promise((resolve, reject) =>  {
          return this.httpClient.post(`${this.api}/collectCarData.php`, siv).pipe(map(
            (reponse: any) => {
              console.log(11111);
              this.modelCar = reponse.modelCar;
              // localStorage.setItem('modelCar', JSON.stringify(this.modelCar));
              this.brandCar = reponse.brandCar;
              // localStorage.setItem('brandCar', JSON.stringify(this.brandCar));
              this.colorCar = reponse.colorCar;
              // localStorage.setItem('colorCar', JSON.stringify(this.colorCar));
              this.versionCar = reponse.versionCar;
              // localStorage.setItem('versionCar', JSON.stringify(this.versionCar));
              this.dateOfCirculationCar = reponse.dateOfCirculationCar;
              // localStorage.setItem('dateOfCirculationCar', JSON.stringify(this.dateOfCirculationCar));
              this.motorizationCar = reponse.motorizationCar;
              // localStorage.setItem('motorizationCar', JSON.stringify(this.motorizationCar));
            },
            resolve({modelCar: this.modelCar, brandCar: this.brandCar, colorCar: this.colorCar, versionCar: this.versionCar, dateOfCirculationCar: this.dateOfCirculationCar, motorizationCar: this.motorizationCar})
          )
          );
        });
        return promise;
    et apparament il ne rentre pas dans la fonction car le console.log n'apparait jamais

  4. #4
    Membre très actif
    Homme Profil pro
    Développeur Web
    Inscrit en
    Janvier 2019
    Messages
    707
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2019
    Messages : 707
    Par défaut
    regarde le code en vert
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
     addSIV(siv: Siv): Promise<any> {
        let promise = new Promise((resolve, reject) =>  {
          return this.httpClient.post(`${this.api}/collectCarData.php`, siv).pipe(map(
            (reponse: any) => {
    
        console.log("reponse", reponse);
               // this.modelCar = reponse.modelCar;
              // localStorage.setItem('modelCar', JSON.stringify(this.modelCar));
              const brandCar = reponse.brandCar;
              const  colorCar = reponse.colorCar;
              const  versionCar = reponse.versionCar;
              const  dateOfCirculationCar = reponse.dateOfCirculationCar;
              const  motorizationCar = reponse.motorizationCar;
    
            resolve({modelCar: this.modelCar, brandCar: this.brandCar, colorCar: this.colorCar, versionCar: this.versionCar, dateOfCirculationCar: this.dateOfCirculationCar, motorizationCar: this.motorizationCar})
            },
            resolve({modelCar: this.modelCar, brandCar: this.brandCar, colorCar: this.colorCar, versionCar: this.versionCar, dateOfCirculationCar: this.dateOfCirculationCar, motorizationCar: this.motorizationCar})
          )
          );
        });
        console.log(promise);
        return promise;
      }

  5. #5
    Membre confirmé
    Femme Profil pro
    Développeur Web
    Inscrit en
    Mars 2021
    Messages
    82
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Mars 2021
    Messages : 82
    Par défaut
    dans ma console j'ai uniquement cette réponse

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    data {modelCar: undefined, brandCar: undefined, colorCar: undefined, versionCar: undefined, dateOfCirculationCar: undefined,*}

  6. #6
    Membre très actif
    Homme Profil pro
    Développeur Web
    Inscrit en
    Janvier 2019
    Messages
    707
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2019
    Messages : 707
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    addSIV(siv: Siv): Promise<any> {
        let promise = new Promise((resolve, reject) =>  {
          return this.httpClient.post(`${this.api}/collectCarData.php`, siv).subscribe(
            (reponse: any) => {
    
        console.log("reponse", reponse);
               // this.modelCar = reponse.modelCar;
              // localStorage.setItem('modelCar', JSON.stringify(this.modelCar));
              const brandCar = reponse.brandCar;
              const  colorCar = reponse.colorCar;
              const  versionCar = reponse.versionCar;
              const  dateOfCirculationCar = reponse.dateOfCirculationCar;
              const  motorizationCar = reponse.motorizationCar;
    
            resolve({modelCar: this.modelCar, brandCar: this.brandCar, colorCar: this.colorCar, versionCar: this.versionCar, dateOfCirculationCar: this.dateOfCirculationCar, motorizationCar: this.motorizationCar})
            }
          )
    
        });
        return promise;
      }

  7. #7
    Membre confirmé
    Femme Profil pro
    Développeur Web
    Inscrit en
    Mars 2021
    Messages
    82
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Mars 2021
    Messages : 82
    Par défaut
    effectivement cela fonctionne parfaitement en changeant .pipe(map() par .subscribe()

    C'est vrai que je me pars un peu avec tous ces opérateurs de rxjs.

    Merci beaucoup pour ton aide

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Erreur dans construction fonction
    Par abraxis dans le forum Ext JS / Sencha
    Réponses: 0
    Dernier message: 05/01/2011, 13h30
  2. Programme de construction d'une fonction ax2+bx+c
    Par sodjinou dans le forum Pascal
    Réponses: 11
    Dernier message: 13/12/2007, 01h29
  3. Construction de fonction
    Par frol dans le forum Langage
    Réponses: 4
    Dernier message: 16/08/2006, 17h56
  4. Windows Vista Beta 2 : manque de fonctions promises?
    Par repié dans le forum Windows Vista
    Réponses: 1
    Dernier message: 07/02/2006, 09h51
  5. [VB.NET] Difficulté de construction... fonction IIF
    Par Pleymo dans le forum Windows Forms
    Réponses: 4
    Dernier message: 09/02/2005, 21h28

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo