簡體   English   中英

Handlebars {{#each}} 助手顯示我從 Mongoose.model.find() 獲得的錯誤數量的值

[英]Handlebars {{#each}} helper displaying wrong amount of values that i get from Mongoose.model.find()

當我嘗試在我的list.handlebars上使用{{#each}}時,它在顯示我的內容填充數據之前在{{#each}}中顯示了 2 個內容,我試圖弄清楚發生了什么但是從我一直在研究,我沒有看到解決方案。

這是我的代碼的相關部分:

app.js(主要代碼):

app.get("/lista/:arvores", (req, res) => {
    if (req.params.arvores === "arvoresnativas") {
        db.nativas.find().then((dataArvoresNativas) => {
            dataArvoresNativas.forEach((findArvoresNativas) => {
                console.log(findArvoresNativas);
                res.render("lista.handlebars",
                    {
                        findArvNativas: findArvoresNativas,
                        strArv: stringArvoresNativas,
                    }
                );
            });
        });
    }
});

列表.把手

<h1>Lista de {{strArv}}</h1>

{{#each findArvNativas}}
    <h2>Nome: {{nome}}</h2>
    <h2>Nome científico: {{nomecientifico}}</h2>
    <h2>Ordem: {{ordem}}</h2>
    <h2>Origem: {{origem}}</h2>
{{/each}}

這是 mongo.js,mongoose 連接,我在其中導出Moongoose.model()用於我的國家的本地和外來樹種(我是巴西人,對於 var 名稱感到抱歉,我希望有人能理解我的代碼) :

mongo.js

const mongoose = require("mongoose");

mongoose
    .connect("mongodb://localhost/mongodatabase", {
        useNewUrlParser: true,
    })
    .then(() => console.log("Conectado ao MongoDB!"))
    .catch((error) => console.log("Erro ao tentar se conectar ao MongoDB: " + error));

const arvores = mongoose.Schema({
    nome: {
        type: String,
        require: true,
    },

    nomecientifico: {
        type: String,
        require: true,
    },

    ordem: {
        type: String,
        require: true,
    },

    origem: {
        type: String,
        require: true,
    },
});

const arvoresnativas = mongoose.model("arvoresnativas", arvores);
const arvoresexoticas = mongoose.model("arvoresexoticas", arvores);

module.exports = {
    nativas: arvoresnativas,
    exoticas: arvoresexoticas,
};

當我 go 到 http://localhost:3031/lista/arvoresnativas 時,那是我的 output:

HTML 渲染

但看看當我執行db.arvoresnativas.find()時 mongo 查詢顯示的內容:

日志

我試圖刪除表格並重新定義它,我也嘗試了幾乎所有的東西,但我是一個初學者,除了我能找到的谷歌搜索之外,我不能做更多的事情。

您不想遍歷數據並執行 Handlebars 文件的單獨renders 您目前正在此處執行此操作:

dataArvoresNativas.forEach((findArvoresNativas) => {
  // You should only call this once, you currently are calling it for every
  // `findArvoresNativas` you have
  res.render(/* ... */);
});

我的猜測是,這意外地不會對事情產生太大影響,因為您的find操作只返回一個項目,所以您的forEach循環只執行一次。

因此,當您 go 進行渲染時,您將發送一個 Object 作為您的findArvNativas變量,然后您使用內置的each方法對其進行迭代。 Handlebars 的each遍歷 object 中的鍵/值對,這似乎不是您想要的。

相反,只需使用點符號來訪問findArvNativas object 上的嵌套屬性。

<h1>Lista de {{strArv}}</h1>

<h2>Nome: {{findArvNativas.nome}}</h2>
<h2>Nome científico: {{findArvNativas.nomecientifico}}</h2>
<h2>Ordem: {{findArvNativas.ordem}}</h2>
<h2>Origem: {{findArvNativas.origem}}</h2>

如果您不想在每次要訪問其上的屬性時都繼續鍵入findArvNativas ,則可以使用with幫助程序來設置新的上下文。

<h1>Lista de {{strArv}}</h1>

{{#with findArvNativas}}
    <h2>Nome: {{nome}}</h2>
    <h2>Nome científico: {{nomecientifico}}</h2>
    <h2>Ordem: {{ordem}}</h2>
    <h2>Origem: {{origem}}</h2>
{{/with}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM