开发人员大家好,我目前正在做一个项目,需要我从数据库显示我的用户配置文件,但我遇到了问题。我使用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})
})
下面是我的架构代码:
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)
当您收到该错误消息时,您想做什么?您正在尝试以管理员身份登录还是正在使用已知的用户凭据?在 .ejs 文件中,在使用它的属性之前,您应该首先检查foundUser 是否为空或未定义。例如:
<% if (foundUser) { %>
<div>
<img src="<%= foundUser.passport %>" alt="<%= foundUser.fullname %>">
</div>
<% } else { %>
<div>
<p>User information not available.</p>
</div>
<% } %>