我无法解决这个问题,我在网上搜索了很多但没有任何帮助。 我在localhost:63447上运行frontEnd(angular),在localhost:3000(nodejs)上运行backend。 我只有在新用户和节点必须在发回响应之前创建它时才会遇到问题。用户正在创建,但我在角度上得到了一个空洞的响应。 我通过angular发送帖子:
$scope.signIn = function(userToSignIn){
console.log(userToSignIn);
$http.post('http://localhost:3000/authenticate/signup',
userToSignIn)
.success(function(data, status, headers, config){
$scope.test = data;;
})
.error(function(data, status, headers, config){
console.log(data);
});
到nodejs中的函数:
router.post('/signup',
function(req, res, next) {
console.log(req.body);
passport.authenticate('local-signup', function(err, user, info) {
if (err) { return next(err); }//This one is working
else if (!user) { return res.json({status: req.session.flash.signupMessage}); }//work
else if(user) {
return res.status(200).json(user._id);//this is the problem
}
})(req, res, next);}),
这是local-signup函数:
passport.use('local-signup', new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true // allows us to pass back the entire request to the callback
},
function(req, email, password, done) {
// User.findOne wont fire unless data is sent back
process.nextTick(function() {
// 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 }, 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', 'That email is already taken.'));
} else {
// if there is no user with that email
// create the user
var newUser = new User();
// set the user's local credentials
newUser.local.email = email;
newUser.local.password = newUser.generateHash(password);
// save the user
newUser.save(function(err) {
if (err) {
console.log('error'); throw err;
}else
console.log('saved');
return done(null, newUser, req.flash('You were successly logged in'));
});
}
});