简体   繁体   English

Handlebars 返回空字符串

[英]Handlebars returns empty string

Searched the internet for what could be wrong with my code, but couldn't find anything.在互联网上搜索我的代码可能有什么问题,但找不到任何东西。 What exactly am I doing wrong?我到底做错了什么? Could it be that I'm not passing the data to the .hbs file correctly?难道是我没有正确地将数据传递给.hbs文件?

The page is a simple blog website.该页面是一个简单的博客网站。 You post the title and the text - so it appears on the main page.您发布标题和文本 - 所以它会出现在主页上。 This is what the model looks like:这是 model 的样子:

const { Schema, model } = require('mongoose')

const schema = new Schema({
    title: { // A TITLE
        type: String,
        required: true
    },
    content: { // A TEXT OF THE ARTICLE
        type: String,
        require: true
    }
})

module.exports = model('Article', schema)

Next, I want to use this data on my pages and I write the following in the routes:接下来,我想在我的页面上使用这些数据,并在路由中写下以下内容:

const { Router } = require('express')
const articles = require('../models/articles')
const router = Router()

router.get('/', (req, res) => {
    res.render('index', {data: articles}) // HERE I USE THE COLLECTION
})

router.get('/post', (req, res) => {
    res.render('post', {isPost: true})
})

router.post('/post', async (req, res) => {
    const article = new articles({
        title: req.body.title,
        content: req.body.content
    })

    await article.save()
    res.redirect('/')
})

module.exports = router

As you can see above, there are only three routes.正如您在上面看到的,只有三个路线。 The first is the main page, where I use data from the database.第一个是主页,我使用数据库中的数据。 The second is just a form where I go to write an article.第二种只是我在go的表格里写的一篇文章。 Don't take attention to the IsPost variable, it is needed for the button in the header, and everything works there.不要注意IsPost变量,header 中的按钮需要它,并且一切正常。 The third route comes after we confirm the data and they are thrown into the database.第三条路线是在我们确认数据并将它们放入数据库之后。 I look into MongoDB and see that everything is fine.我查看了 MongoDB,发现一切都很好。 The data is really saved, the form works, I'm happy.数据真的保存了,表格有效,我很高兴。

MongoDB 中的集合

Now it remains to display the data.现在它仍然显示数据。 This is where the problem begins.这就是问题的开始。 This is how I display data on the page (it's Bootstrap):这就是我在页面上显示数据的方式(它是 Bootstrap):

// HERE IMPORTANT PART OF CODE BEGINS
{{#if data.length}}
    <div style="max-width: 1240px" class="d-flex justify-content-center mx-auto">
        <div class="d-flex mt-5">
                <div class="d-flex row flex-wrap justify-content-center">
                    {{#each data}}
                        <div class="rounded border bg-light custom-item p-3 m-1">
                            <h4>{{this.title}}</h4>
                            <p class="text">{{this.content}}</p>
                        </div>
                    {{/each}}
                </div>
        </div>
    </div>
    // HERE ENDS IMPORTANT PART OF THE CODE

    <style>
        .custom-item{
            width: 240px;
            height: 240px;
        }

        .text{
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 7;
                    line-clamp: 7; 
            -webkit-box-orient: vertical;
        }
    </style>

{{else}}

Instead of outputting blocks with titles and their full text, it outputs this:它不是输出带有标题及其全文的块,而是输出:

结果

Note.笔记。 Only two blocks with h4 and p tags are displayed, but they are empty.只显示了两个带有h4p标签的块,但它们是空的。 Exactly two blocks are displayed, although there are only three records in the database.尽管数据库中只有三个记录,但恰好显示了两个块。 When I change their number, there will always be two blocks on the page.当我更改他们的号码时,页面上总会有两个块。

And here is the final question.这是最后一个问题。 How does my code work and why does it output exactly two blocks?我的代码是如何工作的,为什么它 output 正好是两个块? How can I fix the code so that every record is retrieved from the database?如何修复代码以便从数据库中检索每条记录? Write if I forgot to add something else.如果我忘记添加其他内容,请写下。

If you want to print all the data present in your collection,如果要打印集合中存在的所有数据,
Then don't this:然后不要这样:

router.get('/', (req, res) => {
    res.render('index', {data: articles})
})

rather do this:而是这样做:

router.get('/', (req, res) => {
    const data = await articles.find().exec()
    res.render('index', {data}) 
})

Hope this helps you and you were looking for this answer!希望这对您有所帮助,并且您正在寻找这个答案!

Finally I found solution!!!终于找到解决办法了!!!

router.get('/', (req, res) => {
    const data = await articles.find({}).lean()
    res.render('index', {data}) 
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM