name属性未定义。我知道这应该是一个简单的解决方案。所以我应该在某个地方定义它吗?但我已检查并检查,我找不到问题。有人可以查看我的代码,看看我的问题是什么?此时我只是构建了数据库。我在那里有很多评论,因为在我开始新的事情之前,这是一个评论应用程序。我正在使用Postman来测试寄存器路由是否正常工作。到目前为止它给我的错误是它无法读取未定义的属性名称。这是整个错误
在postman上,我使用POST方法http://localhost:3000/users/register 没有授权 在标题中,键是Content-Type,值是application / json 所以我很确定我正确使用Postman。
TypeError: Cannot read property 'name' of undefined
at router.post (/Users/jonathanschroeder/Desktop/authapp/routes/users.js:12:24)
at Layer.handle [as handle_request] (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/layer.js:95:5)
at /Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/index.js:335:12)
at next (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/index.js:174:3)
at router (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/index.js:47:12)
at Layer.handle [as handle_request] (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/index.js:317:13)
at /Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/index.js:335:12)
at next (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/index.js:275:10)
at serveStatic (/Users/jonathanschroeder/Desktop/authapp/node_modules/serve-static/index.js:75:16)
模型/ user.js的
const mongoose = require('mongoose');
//bcrpypt for encrpyption
const bcrypt = require('bcryptjs');
//to connect to database
const config = require('../config/database');
//Create the Schema
const UserSchema = mongoose.Schema({
name: {
type: String
},
email: {
type: String,
require: true,
unique: true //no two users can share the same email address.
},
username: {
type: String,
require: true
},
password: {
type: String,
require: true
}
});
//Create variable that can be used outside. set that to mongoose model and pass in the UserSchema
const User = module.exports = mongoose.model('User', UserSchema);
//Create two functions. Get the user by ID and get the user by username. Of course I need to do module.exports to use it outside.
module.exports.getUserById = function(id, callback){
User.findById(id, callback);
}
module.exports.getUserByUsername = function(username, callback){
const query = {username:username} //findOne function takes in query
User.findOne(query, callback);
}
//pass in 10 which means Number of rounds to use. (Default is 10 anyways but I will pass 10)
//reference in case I forget www.npmjs.com/package/bcryptjs
//so it is confusing but this returns the hashed password.
//https://busy.org/@nafestw/mean-tutorial-part-2-adding-a-user-model was a great tutorial for this.
module.exports.addUser = function(newUser, callback){ //I did the callback in the route user.js where it gives success or fail and res.sends success or fail.
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(newUser.password, salt, (err, hash) => {
if(err) throw err;
newUser.password = hash;
newUser.save(callback);
});
});
}
路由/ users.js
const express = require('express');
const router = express.Router();
const passport = require('passport');
const jwt = require('jsonwebtoken');
//Now that I created the model I will bring it in here.
const User = require('../models/user');
//Registration
router.post('/register', (req,res,next) =>{
//res.send('registration');
let newUser = new User({
name: req.body.name,
email: req.body.email,
username: req.body.username,
password: req.body.password //I will run this password through bcrypt.hash which will has before db.
});
User.addUser(newUser, (err, user) =>{ //I will create this addUser function inside the models user.js
if(err){
res.json({success:false, msg:'Registration Failed!'})
}else{
res.json({success:true, msg:'User is Registered!'})
}
});
});
module.exports = router;
这是app.js
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const cors = require('cors');
const passport = require('passport');
const mongoose = require('mongoose');
const config = require('./config/database')
mongoose.connect(config.database);
mongoose.connection.on('connected',function(){console.log('yay i am connected to database'+config.database)});
mongoose.connection.on('error',function(error){console.log('You have an error'+error)});
const app = express();
const users = require('./routes/users');
const port = 3000;
app.use(cors());
app.use(express.static(path.join(__dirname, 'public')))
app.get('/', function(req,res){res.send('Sending Response')})
app.use('/users', users)
app.listen(port, function(){console.log('Server started on port '+port)})
app.use(bodyParser.json());
这是我为邮递员准备的内容
{
"name":"John Doe",
"email":"john@doe.com",
"username":"johndoe",
"password":"123456"
}
我认为这几乎是我能提供的所有细节。但如果您需要更多信息,请告诉我。我创建的唯一其他文件是数据库文件 配置/ database.js
module.exports = {
database:'mongodb://localhost:27017/authapp',
secret:'NintamaRantaro'
}
我希望你能找到我的问题。我会一直看着
答案 0 :(得分:0)
为了阅读帖子请求的正文,你需要身体解析器。看这里: body-parser
并在用户路线中添加
const bodyParser = require('body-parser')
// create application/json parser
app.use(bodyParser.json())
// create application/x-www-form-urlencoded parser
app.use(bodyParser.urlencoded({ extended: false }))
取决于您发布数据的方式