0
バックエンド(node.js)アプリにpassport.jsを使用してユーザーにサインインしています。私は常に次のエラーに遭遇しています:Json at position error
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)
私はサインアップしています。私のコードで何が間違っていますか?
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);
}
}
私のNode.jsアプリ:あなたが代わりにJSON
応答のxml
応答を取得している
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);
});
}
});
}));
私はMongoDB Mongooseを使ってデータを取得しているので、私はそうは思わない。 –
ネットワークをチェックアウトします。レスポンスをJSONにマップしようとしている間、map((res:any)=> res.json())でエラーが発生します。あなたは確かにxml応答を得ています。 – omeralper
Ok thanx。 resのみ動作する –