我正在尝试在Nodejs中创建一个rest API。我已经定义了我的模型并在我的路线中参考了我的模型并使用了该模型。我在下面给出了所有代码详细信息。请有人帮助我。
这是我的路由器文件:index.js
const routes = require('express').Router();
const User=require('./model');
const bcrypt=require('bcryptjs');
const config=require('../config');
routes.post('/register',function(req,res){
var hashPassword=bcrypt.hashPassword(req.body.password,8);
User.create({
name:req.body.name,
password:hashPassword,
admin:req.body.admin
},function(err,user){
if(err){
return res.status(500).send("There is the problem with registering");
}
var payload={
"id":"user._id"
}
var token=jwt.sign(payload,config.secret,{
expiresIn: 86400
})
res.status(200).send({
auth:true,
token:token
})
})
})
这是我的模型:model.js
// get an instance of mongoose and mongoose.Schema
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// set up a mongoose model and pass it using module.exports
module.exports = mongoose.model('User', new Schema({
name: String,
password: String,
admin: Boolean
}));
这是我的server.js:
// Bring in our dependencies
const app = require('express')();
const routes = require('./routes');
const config=require('./config');
const mongoose=require('mongoose');
const bodyparser=require('body-parser');
const cluster=require('cluster');
const numCPUs = require('os').cpus().length;
// Connect all our routes to our application
if(cluster.isMaster){
console.log('Master process running');
for(var i=0;i<=numCPUs;i++){
cluster.fork(i);
}}else{
app.use('/api', routes);
app.set('port', 3000);
app.use(bodyparser.urlencoded({extended:false}));
app.use(bodyparser.json());
app.listen(app.get('port'));
console.log('Server listening on port ' + app.get('port'));
}
mongoose.connect(config.database);
mongoose.connection.on('connected',()=>{
console.log("Database Connected");
})
mongoose.connection.on('error',(err)=>{
if(err){
console.log(err);
}
})
cluster.on('exit',(worker,code,signal)=>{
console.log('worker %d died (%s). Restarting...',worker.process.pid,code||signal);
cluster.fork();
})
当我尝试从邮递员发布数据时,我遇到了错误。
TypeError: Cannot read property 'password' of undefined
at F:\project\node-project\jwt-auth\routes\index.js:14:49
at Layer.handle [as handle_request] (F:\project\node-project\node_modules\express\lib\router\layer.js:95:5)
at next (F:\project\node-project\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (F:\project\node-project\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (F:\project\node-project\node_modules\express\lib\router\layer.js:95:5)
at F:\project\node-project\node_modules\express\lib\router\index.js:281:22
at Function.process_params (F:\project\node-project\node_modules\express\lib\router\index.js:335:12)
at next (F:\project\node-project\node_modules\express\lib\router\index.js:275:10)
at Function.handle (F:\project\node-project\node_modules\express\lib\router\index.js:174:3)
at router (F:\project\node-project\node_modules\express\lib\router\index.js:47:12)
at Layer.handle [as handle_request] (F:\project\node-project\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (F:\project\node-project\node_modules\express\lib\router\index.js:317:13)
at F:\project\node-project\node_modules\express\lib\router\index.js:284:7
我无法找到我犯错的地方?有人请帮我识别错误。
答案 0 :(得分:3)
我无法找到我犯错的地方?有人请帮我识别错误
错误与快速堆栈的工作方式有关。您在快递中注册中间件,路由等事宜的顺序。
Express应用程序本质上是一系列中间件函数调用。
将快速应用程序可视化为一系列函数调用串联可能会有所帮助。
这意味着当您执行以下操作时:
const routes = require('./routes');
app.use('/api', routes);
.
.
.
app.use(bodyparser.json());
您说要在堆栈中的路由之后使用body-parser
,因此您获得错误TypeError: Cannot read property 'password' of undefined
的原因与路由定义一样,req.body
尚未填充body-parser
。
通常有两种解决方法:
1)更改顺序:
app.use(bodyparser.json())
.
.
.
app.use('/api', routes);
2)在路线文件中使用body-parser
作为中间件:
const routes = require('express').Router();
const bodyParser = require('body-parser')
const jsonParser = bodyParser.json()
routes.post('/register', jsonParser, function(req,res){
...
})