【发布时间】:2015-07-07 18:14:35
【问题描述】:
我正在尝试在 NodeJS 和 AngularJS 中构建一个简单的链接共享网络应用程序。我有一个函数存在,但被报告为未定义。
这是 angularApp.js 中特定于此操作的代码
app.factory('auth', ['$http', '$window', function($http, $window){
var auth = {};
auth.register = function(user){
return $http.post('/register', user).success(function(data){
auth.saveToken(data.token);
}).error(function(err, req, res, next) {console.log(err)});
};
return auth;
}]);
这是在 routes\index.js 中注册路由的代码
var express = require('express');
var jwt = require('express-jwt');
var router = express.Router();
var auth = jwt({secret: 'SECRET', userProperty: 'payload'});
var mongoose = require('mongoose');
var User = mongoose.model('User');
// Creates a user given a username and password
router.post('/register', function(req, res, next){
if(!req.body.username || !req.body.password){
return res.status(400).json({message: 'Please fill out all fields'});
}
var user = new User();
user.username = req.body.username;
user.setPassword(req.body.password);
user.save(function (err){
if(err){ return next(err); }
return res.json({token: user.generateJWT()})
});
});
Users.js
var mongoose = require('mongoose');
var jwt = require('jsonwebtoken');
var UserSchema = new mongoose.Schema({
username: {type: String, lowercase: true, unique: true},
hash: String,
salt: String
});
mongoose.model('User', UserSchema);
// Accepts a password then generates a salt and associated password hash
UserSchema.methods.setPassword = function(password) {
...
};
这里是有问题的 HTML 文件代码:
<form ng-submit="register()" style="margin-top:30px;">
<input type="text" class="form-control" placeholder="Username" ng-model="user.username"></input>
<input type="password" class="form-control" placeholder="Password" ng-model="user.password"></input>
<button type="submit" class="btn btn-primary">Register</button>
</form>
由于我在 angularApp.js 中有 }).error(function(err, req, res, next) {console.log(err)});,所以当我单击注册按钮时,我会在控制台中得到以下输出:
<h1>undefined is not a function</h1>
<h2></h2>
<pre>TypeError: undefined is not a function
at C:\Linked\linked\routes\index.js:107:8
...
</pre>
C:\Linked\linked\routes\index.js:107:8 是以下行:user.setPassword(req.body.password);
console.log() 不会在 routes\index.js 中输出任何内容,无论我放什么(甚至是console.log("hi");),所以我无法检查req.body 的值。
【问题讨论】:
-
第 107 行上方抛出单词“debugger;”它应该在该点之前捕获调试器,以便您可以评估它。
-
感谢您的建议,但添加该行并没有任何区别。如果我尝试“暂停捕获的异常”,则 chrome 的“源”选项卡会在 angular.js 处停止,特别是在它显示的行:
throw $injectorMinErr('nomod', "Module '{0}' is not available! You either misspelled " + "the module name or forgot to load it. If registering a module ensure that you " + "specify the dependencies as the second argument.", name);但我不知道{0}应该是什么!
标签: javascript angularjs node.js express