无法读取未定义的属性“forEach”

时间:2021-05-28 11:42:23

标签: node.js mongodb express ejs

我正在使用 nodejs、expressjs 和 mongodb 开发一个博客网络应用程序,使用 ejs 进行视图。 当访问者单击用户的帖子之一时,我想在显示页面上显示用户的所有帖子的标题。我收到此错误'user.sermons.forEach(function(author){%> 107|

108| <%=author.sermon.title %> 109|

无法读取属性 'forEach ' 下面的 undefined` 是我的代码

======================布道/show.ejs====================== ==========================

 <div class="widget-post">
       <h5>RECENT POST from The Same Author</h5>
        <ul class="list-unstyled">
                                    
            <li><i class="fa fa-angle-right"></i><a href="">
                                           
                  <%user.sermons.forEach(function(author){%>
                         <p><%=author.sermon.title %></p>

                    <%}) %> 
              </li>
            </ul>
        </div>
                                    
=======================model/users.js======================================
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const passportLocalMongoose = require('passport-local-mongoose')
const imageSchema = new Schema({
    url: String,
    filename: String

});
imageSchema.virtual('thumbnail').get(function () {
    return this.url.replace('/upload', '/upload/w_395,h_480')
})
const opts = { toJSON: { virtuals: true } }
const userSchema = new Schema({
    email: {
        type: String,
        required: true,
        unique: true
    },
    images: [imageSchema],
    firstname: {
        type: String,
        required: true
    },
    lastname: {
        type: String,
        required: true
    },
    phone: {
        type: Number,
        unique: true
    },
    description: String,
    location: String,
    department:String,
    isVerified: Boolean,
    emailToken: String,
    resetPasswordToken: String,
    resetPasswordExpires: Date,
    sermons: {
        
            type: Schema.Types.ObjectId,
            ref: 'Sermon'   
    }
}, opts);
userSchema.plugin(passportLocalMongoose);
module.exports = mongoose.model('User', userSchema)


============models/sermons===========================
const mongoose = require('mongoose');
const Review = require('./reviews')
const Schema = mongoose.Schema;
const imageSchema = new Schema({
    url: String,
    filename: String

});
imageSchema.virtual('thumbnail').get(function () {
    return this.url.replace('/upload', '/upload/w_300,h_200')
})
const opts = { toJSON: { virtuals: true } }
const sermonSchema = new Schema({
    title: String,
    images: [imageSchema],
    bibleText: String,
    content: String,
    author: {
        type: Schema.Types.ObjectId,
        ref: 'User'
    },
    createdAt: { type: Date, default: Date.now },
    reviews: [
        {
            type: Schema.Types.ObjectId,
            ref: 'Review'
        }
    ]
}, opts)

sermonSchema.post('findOneAndDelete', async function (doc) {
    if (doc) {
        await Review.deleteMany({
            _id: {
                $in: doc.reviews
            }
        })
    }
})
module.exports = mongoose.model('Sermon', sermonSchema)

======================routes/sermon.js============================
// SHOW ROUTE
router.get('/:id', async (req, res) => {
    const sermon = await Sermon.findById(req.params.id).populate({
        path: 'reviews',
        populate: {
            path: 'author'
        }
    }).populate('author');
  
    if (!sermon) {
        req.flash('error', 'Cannot find that Sermon')
        return res.redirect('/sermons')
    }
    const user = await User.find().where('sermons').equals(sermon._id).exec()
    console.log(user)
    // console.log(sermon)
    res.render('sermon/show', { sermon, user })
})

0 个答案:

没有答案