0
我使用passport.js爲我的後端(node.js)應用程序來註冊用戶。我總是會出現以下錯誤:Json在位置錯誤
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應用:
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
好的。只有水庫工程 –