我对JS,Angular.js和node.js非常新。我正在尝试登录注册项目,我有一点问题。
这是我的代码:
login.ctrl.js:
var app = angular.module('login-register', []);
app.factory('UserLog', ['$http', function ($http) {
return {
loginUser: function (user, callback) {
$http.get('/user', user)
.success(function (data, status) {
callback(data, false);
}).
error(function (data, status) {
callback(data, true);
});
}
}
}]);
app.controller('LoginCtrl', ['$scope', 'UserLog', function ($scope, UserLog) {
$scope.logUser = function () {
var user = {
'username': $scope.username,
'password': $scope.password
};
UserLog.loginUser(user, function (result, error) {
if (error) {
window.alert('Error finding user in DB');
return;
}
if (!result) {
window.alert('Invalid username or password');
return;
}
window.location.href = "views/chat_screen.html";
});
}
}]);
server.js:
app.get('/user', function (req, res) {
// Find user by userName
User.findOne({ username: req.body.username }, function (err, userById) {
if (err) return console.log(err);
if (!userById) return console.log('user was not found');
res.send(userById);
})
});
('用户'是猫鼬的模式)。
运行此代码时,req.body.username
未定义。
你们当中有些人可以解释我该怎么做..?
谢谢:)
答案 0 :(得分:0)
您正在使用" get"请求,它不能发送请求的身体,使用" post"请求类型以发送正文元素。