我目前正在做一个项目,将需要我从数据库中显示我的用户配置文件,但我遇到了一个问题。我使用expressjs和mongoDB作为数据库。我会很感激这里的解决方案。
下面是我的服务器代码。
const express = require('express')
const app = express()
const mongoose = require('mongoose');
function connectDB() {
try{
console.log("connection to db")
mongoose.connect('mongodb://127.0.0.1:27017/db')
console.log("connected")
} catch (error) {
console.log(error)
}
}
connectDB()
const User = require('./model/reguser')
let foundUser
app.post('/login', async (req, res)=>{
const {username, password} = req.body
foundUser = await User.findOne({username:username})
if(foundUser){
const user = await bcrypt.compare(password, foundUser.password)
if (user){
res.redirect('/')
}else{
req.flash('info', 'Username or Password incorrect')
res.redirect('/login')
}
}else{
const foundAdmin = await Admin.findOne({username:username})
if(foundAdmin){
const user = await bcrypt.compare(password, foundAdmin.password)
if(user){
res.redirect('/admin')
}else{
req.flash('info', 'Username or Password incorrect')
res.redirect('/login')
}
}
}
})
app.get('/', (req, res )=>{
res.render('dashboard.ejs', {foundUser})
})
下面是我的schema代码:
const mongoose = require('mongoose');
const { Schema } = mongoose;
const users = new Schema ({
username:{
type:String,
unique: true,
trim:true,
minlength:[10, 'Username must be more than 10'],
required: true
},
password:{
type:String,
required: true,
trim:true,
minlength:[10, 'Password must be more than 8']
},
fullname:{
type:String,
required: true,
trim:true,
minlength:[10, 'Fullname must be more than 8']
},
passport:{
type:String,
required: true,
trim:true,
minlength:[10, 'Passport must be more than 8']
},
phone:{
type:String,
unique: true,
required: true,
trim:true,
minlength:[10, 'Phone be more than 8']
},
role:String,
active:Boolean
})
module.exports = mongoose.model('User', users);
但我确实收到了这个错误信息
TypeError: C:\Users\go\Downloads\mongo-class\views\dashboard.ejs:117
115| <div class="profile">
116|
>> 117| <div><img src=<%= foundUser.passport %> alt=<%= foundUser.fullname %>></div>
118| <div class="fullname"><p><%=foundUser.fullname %></p></div>
119| <div class="phone"><p><%= foundUser.phone %></p></div>
120| <div class="name"><p><%=foundUser.email %></p></div>
Cannot read properties of undefined (reading 'passport')
at eval ("C:\\Users\\go\\Downloads\\mongo-class\\views\\dashboard.ejs":13:36)
at dashboard (C:\Users\go\Downloads\mongo-class\node_modules\ejs\lib\ejs.js:703:17)
at tryHandleCache (C:\Users\go\Downloads\mongo-class\node_modules\ejs\lib\ejs.js:274:36)
at View.exports.renderFile [as engine] (C:\Users\go\Downloads\mongo-class\node_modules\ejs\lib\ejs.js:491:10)
at View.render (C:\Users\go\Downloads\mongo-class\node_modules\express\lib\view.js:135:8)
at tryRender (C:\Users\go\Downloads\mongo-class\node_modules\express\lib\application.js:657:10)
at Function.render (C:\Users\go\Downloads\mongo-class\node_modules\express\lib\application.js:609:3)
at ServerResponse.render (C:\Users\go\Downloads\mongo-class\node_modules\express\lib\response.js:1039:7)
at C:\Users\go\Downloads\mongo-class\app.js:139:9
at Layer.handle [as handle_request] (C:\Users\go\Downloads\mongo-class\node_modules\express\lib\router\layer.js:95:5)
1条答案
按热度按时间dw1jzc5e1#
当你收到错误消息时,你想做什么?你试图登录作为管理员或你使用一个已知的用户凭据?在你的.ejs文件中,在使用它的属性之前,你应该首先检查foundUser是否为null或undefined。例如: