【问题标题】:ForEach not working on mongodb but it work fine in locally array declarationForEach 不能在 mongodb 上工作,但在本地数组声明中工作正常
【发布时间】:2019-01-05 19:05:51
【问题描述】:

error of the code

我的 forEach 循环在尝试从数据库 (mongodb) 获取数据时无法在 ejs 文件中工作,它包含一个名为 campground 的集合。但是当我将对象集合存储在 myapp.js 代码中的数组中时,它可以正常工作。但是当我尝试通过在数据库中存储数据来实现时,它会显示错误 campground.forEach is not a function

这是我的代码:

myapp.js

var express=require("express");
var app=express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended:true}));
var mongoose = require('mongoose');
mongoose.connect("mongodb://localhost/campit");
var campgroundSchema=new mongoose.Schema({
  name:String,
  image:String
});

var campground=mongoose.model("campground",campgroundSchema);
app.get("/create",function(req,res){
   campground.find({},function(err,campgrounnd){
      if(err){
        console.log(err);
      }
      else{
        res.render("index.ejs",{campground:campground});
      }
    })
 });
app.listen("3000",function(){
  console.log("listening from server 3000"); 
});

index.ejs 文件,它应该被渲染并且它包含那个 for.Each 循环

  <div class="container">
  <div class="row no-gutters">
    <% campground.forEach(function(camp){ %>
      <div class="col col-lg-4 img-thumbnail">
        <img class="but" height="75%" width="100%" src="<%=camp.image%>" alt="image">
        <div class="text-center">
          <h6><%= camp.name %> </h6>
          <button class="btn btn-outline-info " type="button" name="more info"> more info</button>
        </div>
      </div>
    <% }); %>
  </div>
</div>

任何帮助都将受到高度评价..谢谢 console.log the campground

【问题讨论】:

  • 您能否添加您遇到的错误
  • 请看问题的开头
  • 将这一行 console.log(campground) 放在 res.render("index.ejs",{campground:campground}); 之前,看看打印了什么。
  • 它显示了很多我不知道从哪里来的 json 数据,我应该发布它的图像吗??
  • 请张贴我认为您在 campgrounnd 中获得了 mongoDB 光标。

标签: node.js mongodb ejs


【解决方案1】:

MongooseJS 中的 find 方法返回一个 Query 对象(引用)。您可以通过多种方式进行搜索并返回结果。

   campground.find().exec(function(err,result){
      if(err){
        console.log(err);
      }
      else{
        res.render("index.ejs",{campground:result});
      }
   });

【讨论】:

  • 你在哪里尝试访问结果?
  • 在 index.ejs 文件中...我也用 index.ejs 文件中的结果替换了露营地
  • 不,您仍然必须将其作为露营地访问,如您所见,我仍然在这里将其作为露营地传递 --- res.render("index.ejs",{campground:result}) ;
  • 是的,它有效:) :) :) :) finalllllllllllllly 但我不明白.exec 部分为什么使用它?初始代码有什么问题?
  • 如果对您有用,请将答案标记为正确的赞成票,请阅读文档以获取详细说明mongoosejs.com/docs/api.html#model_Model.find
【解决方案2】:

我认为你的变量名有冲突

var campground=mongoose.model("campground",campgroundSchema); // here is a campground
app.get("/create",function(req,res){
   campground.find({},function(err,result /* <-- */){ // the result is also campgrounnd
      if(err){
        console.log(err);
      }
      else{
        res.render("index.ejs",{campground:result/* <-- */}); // pass result instead campgrounnd
      }
    })
 });

【讨论】:

  • 你有console.log(result) 吗?结果和你在问题中附上的图片一样吗??
猜你喜欢
  • 1970-01-01
  • 2018-03-07
  • 1970-01-01
  • 1970-01-01
  • 2018-05-06
  • 1970-01-01
  • 1970-01-01
  • 2011-11-11
  • 2019-10-12
相关资源
最近更新 更多