在mongoDB上,为什么.findById正常工作时.find方法在这里不起作用?

时间:2018-08-27 04:49:15

标签: mongodb ejs

我正在研究在html和mongodb上使用EJS作为数据库和NodeJS的项目。
它假定当某人单击照片以获取更多信息时,将根据数据库中该照片的ID将其重定向到另一页,有关该照片的更多信息将从数据库中接收并传递到html.ejs文件中,显示它们,下面是代码:

app.get("/campgrounds/:id", function(req, res){
    Campground.findById(req.params.id, function(err, foundCampground){
        if(err){
            console.log("can't find this campground");
        } else {
            res.render("show", { foundCampground: foundCampground });
        }
    });
});

这是我在HTML上的代码:

<div class="container">
    <div class="">
        <h2><%=foundCampground.name%></h2>
        <img src="<%=foundCampground.image%>">
        <p><%=foundCampground.describe%></p>
    </div>
</div>

我的问题是,当我使用JS代码时 .findByID方法,一切正常,将显示数据。

但是如果我使用.find({_ id:req.params.id},函数,它将无法显示任何内容。
我试图检查是否从数据库接收到数据并将其正确传递到HTML文件,我发现如果我说像

<p> foundCampground </p>

它将显示一个关于foundCampground的所有数据的对象,但是如果我使用了点符号,如

<p> foundCampground.name </p>

它永远都行不通,为什么会这样?

0 个答案:

没有答案