首页 > 解决方案 > 从我的 ejs 文件中收到错误“无法读取未定义的属性‘用户名’”

问题描述

我正在使用 MERN 堆栈开发电子商务网站。如果用户是否登录,我想根据条件显示两个不同的导航栏!我已经res.locals在我的 app.js 文件中使用将数据公开给所有模板,但是当我在我的 index.ejs 文件中使用这些数据时user.username,它向我显示了上述错误。

我被困在这个问题中并且已经搜索了一整天但无法得到任何解决方案。

这是我的 app.js 文件

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var session = require('express-session');
var mongoose = require('mongoose');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');


var app = express();

app.use(session({
    name: 'uid',
    secret: 'LoginSession',
    saveUninitialized: false,
    resave: false,
    cookie: {
        expires: 6000000
    }
}))

app.use(function(req,res,next){
    res.locals.user = req.session.user;
    next();
});

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static('public'));

app.use('/', indexRouter);
app.use('/users', usersRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};

    // render the error page
    res.status(err.status || 500);
    res.render('error');
});

module.exports = app;

这是我的 index.ejs 文件

<!DOCTYPE html>
<html lang="en">

<head>

    <% include partials/head %>
</head>

<body>

    <% if (!user.username) { %>
        <% include partials/navhome %>
        <%} else { %>
            <% include partials/navhome1 %>
        <% } %>
    
    <div class="main-banner header-text" id="top">
        <div class="Modern-Slider">
            <!-- Item -->
            <div class="item item-1" style="background-image: url(../images/slider-image-1-1920x900.jpg);">
                <div class="img-fill">
                    <div class="text-content">
                        <h6>New Arrival!</h6>
                        <h4>Iphone 12 Pro</h4>
                        <a href="#" class="filled-button">Shop Now</a>
                    </div>
                </div>
            </div>
            <!-- // Item -->
            <!-- Item -->
            <div class="item item-2" style="background-image: url(../images/slider-image-2-1920x900.jpg);">
                <div class="img-fill">
                    <div class="text-content">
                        <h6>New Arrival!</h6>
                        <h4>Samsung S21 Ultra 5G</h4>
                        <a href="#" class="filled-button">Shop Now</a>
                    </div>
                </div>
            </div>
            <!-- // Item -->
            <!-- Item -->
            <div class="item item-3" style="background-image: url(../images/slider-image-3-1920x900.jpg);">
                <div class="img-fill">
                    <div class="text-content">
                        <h6>New Arrival!</h6>
                        <h4>Oneplus 9 pro 5G</h4>
                        <a href="#" class="filled-button">Shop Now</a>
                    </div>
                </div>
            </div>
            <!-- // Item -->
        </div>
    </div>
    <!-- Banner Ends Here -->

    <div class="services">
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <div class="section-heading">
                        <h2>Featured <em>Smart-Phones</em></h2>
                        <span>Phones with best prices and specifications</span>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="service-item">
                        <img src="./images/product-1-720x480.jpg" alt="">
                        <div class="down-content">
                            <h4>Apple Iphone 7</h4>
                            <div style="margin-bottom:10px;">
                                <span>Rs </sup>35000.00 </span>
                            </div>

                            <p>The Iphone 7 attributes 4.7 inch screen, 12 MP back and 7 MP front camera. It comes with iOS 10.0.1 which is upgradable to iOS 11.2.5 and runs on 2.34GHz processor.</p>
                            <a href="product-details.html" class="filled-button">Shop Now</a>
                        </div>
                    </div>

                    <br>
                </div>
                <div class="col-md-4">
                    <div class="service-item">
                        <img src="./images/product-2-720x480.jpg" alt="">
                        <div class="down-content">
                            <h4>Realme 5 pro</h4>
                            <div style="margin-bottom:10px;">
                                <span>Rs </sup>32000.00 </span>
                            </div>

                            <p>The new Realme 5 Pro is packed with a powerful chipset and high-quality camera setup. The handset is powered by Snapdragon 712 chipset.</p>
                            <a href="product-details.html" class="filled-button">Shop Now</a>
                        </div>
                    </div>

                    <br>
                </div>
                <div class="col-md-4">
                    <div class="service-item">
                        <img src="./images/product-3-720x480.jpg" alt="">
                        <div class="down-content">
                            <h4>Apple Iphone 11 pro</h4>
                            <div style="margin-bottom:10px;">
                                <span>Rs </sup>254,600.00 </span>
                            </div>

                            <p>Apple iPhone 11 Pro Max is packed with high-end specs and will be the flagship of the series. The phone is powered by A13 chip.</p>
                            <a href="product-details.html" class="filled-button">Shop Now</a>
                        </div>
                    </div>

                    <br>
                </div>
                <div class="col-md-4">
                    <div class="service-item">
                        <img src="./images/OnePlus-Nord.jpg" alt="">
                        <div class="down-content">
                            <h4>One Plus Nord</h4>
                            <div style="margin-bottom:10px;">
                                <span>Rs </sup>54,500.00 </span>
                            </div>

                            <p>This device will be powered by one of the latest chipsets available for smartphones in the market that is called Qualcomm SM6350 Snapdragon 690 5G.</p>
                            <a href="product-details.html" class="filled-button">Shop Now</a>
                        </div>
                    </div>

                    <br>
                </div>
                <div class="col-md-4">
                    <div class="service-item">
                        <img src="./images/SamsungGalaxyS20Ultra__1_.jpg" alt="">
                        <div class="down-content">
                            <h4>Samsung Galaxy S20</h4>
                            <div style="margin-bottom:10px;">
                                <span>Rs </sup>170,999.00 </span>
                            </div>

                            <p>Samsung Galaxy S20's chipset is paired with 8 gigabytes of RAM. The high-end RAM and chipset will the processing speed of the smartphone very fast.</p>
                            <a href="product-details.html" class="filled-button">Shop Now</a>
                        </div>
                    </div>

                    <br>
                </div>
                <div class="col-md-4">
                    <div class="service-item">
                        <img src="./images/huawei-p40-black.jpg" alt="">
                        <div class="down-content">
                            <h4>Huawei P40 Pro</h4>
                            <div style="margin-bottom:10px;">
                                <span>Rs </sup>179,999.00 </span>
                            </div>

                            <p>P40 pro has 8 GB of RAM and Krin 990 powerfull processor. It supports wireless charging and equipped with 5500mAh battery capacity.</p>
                            <a href="product-details.html" class="filled-button">Shop Now</a>
                        </div>
                    </div>

                    <br>
                </div>
            </div>
        </div>
    </div>

    <div class="fun-facts">
        <div class="container">
            <div class="more-info-content">
                <div class="row">
                    <div class="col-md-6">
                        <div class="left-image">
                            <img src="./images/about-1-570x350.jpg" class="img-fluid" alt="">
                        </div>
                    </div>
                    <div class="col-md-6 align-self-center">
                        <div class="right-content">
                            <h2>Get to know about <em>our shop</em></h2>
                            <p>Our shop is recognised as the world no 1 selling smart phone shop because we provides best deals and original products!</p>
                            <a href="about.html" class="filled-button">Read More</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="testimonials">
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <div class="section-heading">
                        <h2>What they say <em>about us</em></h2>
                        <span>feedback from our greatest customers</span>
                    </div>
                </div>
                <div class="col-md-12">
                    <div class="owl-testimonials owl-carousel">

                        <div class="testimonial-item">
                            <div class="inner-content">
                                <h4>George Walker</h4>
                                <span>Chief Financial Analyst</span>
                                <p>"Phone E-Shop provided us with the best smart phones in the market under best pricing"</p>
                            </div>
                        </div>

                        <div class="testimonial-item">
                            <div class="inner-content">
                                <h4>John Smith</h4>
                                <span>Market Specialist</span>
                                <p>"Phone E-Shop is the best store right now in smart phone industry which cares about the customer opinoin"</p>
                            </div>
                        </div>

                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Footer Starts Here -->
    <footer>
        <div class="container">
            <div class="row">
                <div class="col-md-3 footer-item">
                    <h4>Phone E-Shop</h4>
                    <p>The store which have all smart phones that you are looking for!</p>
                    <ul class="social-icons">
                        <li><a rel="nofollow" href="https://www.facebook.com" target="_blank"><i class="fa fa-facebook"></i></a></li>
                        <li><a href="https://www.twiter.com"><i class="fa fa-twitter"></i></a></li>
                        <li><a href="https://www.linkedin.com"><i class="fa fa-linkedin"></i></a></li>
                    </ul>
                </div>

                <div class="col-md-3 footer-item">
                    <h4>Additional Pages</h4>
                    <ul class="menu-list">
                        <li><a href="#">Go to top</a></li>
                        <li><a href="#">About Us</a></li>
                        <li><a href="#">Contact Us</a></li>
                    </ul>
                </div>
                <div class="col-md-3 footer-item last-item">
                    <h4>Contact Us</h4>
                    <div class="contact-form">
                        <form id="contact footer-contact" action="" method="post">
                            <div class="row">
                                <div class="col-lg-12 col-md-12 col-sm-12">
                                    <fieldset>
                                        <input name="name" type="text" class="form-control" id="name" placeholder="Full Name" required="">
                                    </fieldset>
                                </div>
                                <div class="col-lg-12 col-md-12 col-sm-12">
                                    <fieldset>
                                        <input name="email" type="text" class="form-control" id="email" pattern="[^ @]*@[^ @]*" placeholder="E-Mail Address" required="">
                                    </fieldset>
                                </div>
                                <div class="col-lg-12">
                                    <fieldset>
                                        <textarea name="message" rows="6" class="form-control" id="message" placeholder="Your Message" required=""></textarea>
                                    </fieldset>
                                </div>
                                <div class="col-lg-12">
                                    <fieldset>
                                        <button type="submit" id="form-submit" class="filled-button">Send Message</button>
                                    </fieldset>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </footer>

    <div class="sub-footer">
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <p>
                        Copyright © 2021 Phone E-Shop - All right reserved.
                    </p>
                </div>
            </div>
        </div>
    </div>

    <% include partials/script %>

</body>

</html>

如果你们中的任何人想要任何其他文件,请告诉我!

先感谢您!

标签: node.jsexpresssession

解决方案


推荐阅读