2017-10-14 13 views
0

J'utilise passport.js pour mon application backend (node.js) pour connecter l'utilisateur. Je reçois toujours l'erreur suivante:Json à l'erreur de position

ERROR SyntaxError: Unexpected token < in JSON at position 0 at Object.parse() at Response.webpackJsonp.../../../http/@angular/http.es5.js.Body.json (http.es5.js:797) at MapSubscriber.project (signup.component.ts:31)

Après avoir actualisé le site, je suis inscrit. Quel est le problème avec mon code?

Voici ma fonction angulaire:

signUp() { 

    this.userNotExist = ""; 
    this.userExist=""; 

     this.http.post('/signup',this.signform.value).map((res: any) => res.json()) 
     .subscribe((res: any) => { 
      console.log('TTTTTTTTT') 
       console.log(res) 
       console.log('TTTTTTTTT') 
       if(res=='EXIST'){ 
        this.userNotExist = ""; 
        this.userExist = 'The email already exists'; 
       }else { 
        this.userExist = ""; 
        this.userNotExist = 'Congrats! You are now signed up'; 
        window.location.reload(); 


       } 
      } 

     ),(error: any) => { 
       console.log(error); 
      } 

} 

app Mon Node.js:

passport.use('local-signup', new LocalStrategy({ 
      // by default, local strategy uses username and password, we will override with email 
      usernameField : 'email', 
      passwordField : 'password', 
      nameMainField : 'nameMain', 
      firstNameField: 'firstName', 
      passReqToCallback : true // allows us to pass back the entire request to the callback 
     }, 
     function(req, email, password, done) { 

      // find a user whose email is the same as the forms email 
      // we are checking to see if the user trying to login already exists 
      User.findOne({ 'local.email' : email }).lean().exec(function(err, user) { 
       // if there are any errors, return the error 

       if (err) 
        return done(err); 

       // check to see if theres already a user with that email 
       if (user) { 
        return done(null, false, req.flash('signupMessage', 'Email already exists.')); 
       } else { 

        var newUserS   = new User(); 

        // set the user's local credentials 
        newUserS.local.email = email; 
        newUserS.local.password = newUserS.generateHash(password); // use the generateHash function in our user model 
        newUserS.local.nameMain = req.body.firstName + ' ' + req.body.nameMain; 
        newUserS.local.firstName = req.body.firstName; 
        newUserS.role=0; 
        newUserS.profileImage='/assets/fonts/male.png'; 

        // save the user 
        newUserS.save(function(err, u) { 
         if (err) 
          throw err; 

         return done(null, newUserS); 
        }); 
       } 
      }); 

     })); 

Répondre

0

Vous obtenez une réponse xml au lieu de réponse JSON. Je suis assez sûr que c'est quelque chose comme ceci:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE paymentService PUBLIC "-//WorldPay//DTD WorldPay PaymentService v1//EN" 
    "http://dtd.WorldPay.com/paymentService_v1.dtd"> 
<paymentService version="1.4" merchantCode="ExampleCode1"> <!--The merchantCode you supplied originally--> 
    <reply> 
    <orderStatus orderCode="ExampleOrder1"> <!--Not present for parse or security violation errors--> 
     <error code="2"> 
     <![CDATA[Invalid address: Postal code is missing or empty.]]> 
     </error> 
    </orderStatus> 
    </reply> 
</paymentService> 

L'exception vient du premier char à la réponse de xml.

+0

Je ne pense pas parce que j'utilise MongoDB Mongoose pour récupérer les données. –

+0

Découvrez votre réseau. Vous obtenez l'erreur exactement à la carte ((res: any) => res.json()) tout en essayant de mapper la réponse à JSON. Vous obtenez une réponse XML, j'en suis sûr. – omeralper

+0

Ok merci. Seulement res fonctionne –