我似乎无法传递密码和一切工作,除了从密码。我useschema我得到的错误,但它可能是我的函数的问题,因为我哈希的密码。我似乎找不到什么问题
我的服务器
const express = require('express')
const colors = require('colors')
const dotenv = require('dotenv').config()
const {errorHandler} = require('./middleware/errorMiddleware')
const connectDB = require('./config/db')
const PORT = process.env.PORT || 5000
//connect to database
connectDB()
const app = express()
app.use(express.json())
app.use(express.urlencoded({extended: false}))
app.get('/', (req, res) => {
res.status(200).json({message: 'Welcome to the support desk api'})
})
app.use('/api/users', require('./routes/userRoutes'))
app.use(errorHandler)
app.listen( PORT, () => console.log(`Port started on ${PORT}`))
我的 Mongoose 图式
const mongoose = require('mongoose')
const userSchema = mongoose.Schema({
name : {
type: String,
required: [true, 'Please enter your name']
},
email : {
type: String,
required: [true, 'Please add an email'],
unique: true
},
password : {
type: String,
required : [true, 'Please add a password'],
},
isAdmin : {
type: Boolean,
required : true,
default: false,
}
},
{
timestamps: true
})
module.exports = mongoose.model('User', userSchema)
我的模型,其中我使用函数注册登录
const asyncHandler = require('express-async-handler')
const bcrypt = require('bcryptjs')
const User = require('../models/userModel')
// @desc register a new user
// @ route api/users
// @acess Public
const registerUser = asyncHandler(async (req, res) => {
const {name, email, password} = req.body
//validation
if(!name || !email || !password) {
res.status(400)
throw new Error('Please iclude all fields')
}
//find if user already exist
const userExists = await User.findOne({email})
if (userExists){
res.status(400)
throw new Error('User already Exist')
}
//hash password
const salt = await bcrypt.genSalt(10)
const hashedPassword = await bcrypt.hash(password, salt)
//create user
const user = await User.create({
name,
email,
hashedPassword,
})
if(user) {
res.status(201).json({
_id: user._id,
name: user.name,
email: user.email
})
} else {
res.status(400)
throw new Error('Invalid user Data')
}
})
// @desc register a new user
// @ route api/users/login
// @acess Public
const loginUser = asyncHandler(async (req, res) => {
res.send('Login Route')
})
module.exports = {
registerUser,
loginUser
}
我的结果来自post man Result from postman
1条答案
按热度按时间vddsk6oq1#
创建后,应使用密码:哈希密码