TypeError:无法使用猫鼬读取未定义的属性"find" [英] TypeError: Cannot read property 'find' of undefined with mongoose

查看:55
本文介绍了TypeError:无法使用猫鼬读取未定义的属性"find"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了一些类似我的问题,但是找不到有效的解决方案,所以我想问一下.

I saw a few questions like mine but couldn't find any solutions that worked so I thought I'd ask.

我正在尝试从数据库中提取所有数据,以便可以在应用程序中选择部分数据.我的数据库工作正常,但是当我尝试拉出图片时失败了,并不断给我这个错误,而且似乎也没有从数据库接收数据:

I'm trying to pull all my data from my database so I can select parts of it in my app. I had my database working fine but when I tried to pull the pictures it failed and keeps giving me this error and also does not seem to receive the data from the database:

TypeError: Cannot read property 'find' of undefined

我已经安装了猫鼬,并且所有东西都可以连接,所以我不知道为什么它无法识别find属性.如果有人有什么想法,我的代码在下面.

I have mongoose installed and everything connects so I can't figure out why it won't recognize the find property. My code is below if anyone has any ideas.

// Dependencies
var express = require("express");
var bodyParser = require("body-parser");
var mongoose = require("mongoose");
var exphbs = require("express-handlebars");
var request = require("request");

// var weapons = require("./weapons.js");

// Set mongoose to leverage built in JavaScript ES6 Promises
mongoose.Promise = Promise;


// Initialize Express
var app = express();

// Use morgan and body parser with our app
app.use(bodyParser.urlencoded({
  extended: false
}));

// Make public a static dir
app.use(express.static(__dirname + "/public"));

app.engine("handlebars", exphbs({ defaultLayout: "main" }));
app.set("view engine", "handlebars");

// Database configuration with mongoose
mongoose.connect("mongodb://localhost/weaponsdb");
var db = mongoose.connection;

// Show any mongoose errors
db.on("error", function(error) {
  console.log("Mongoose Error: ", error);
});

// Once logged in to the db through mongoose, log a success message
db.once("open", function() {
  console.log("Mongoose connection successful.");
});

//routes to home page

app.get('/', function(req, res, next){

    res.render("index",{ title: 'Express' });

});

app.get('/app', function(req, res, next){

    res.render("app",{ layout: 'applayout' });

});

app.get("/all", function(req, res) {

  // Query: In our database, go to the weapon collection, then "find" everything
  db.weapon.find({}, function(error, found) {

    // Log any errors if the server encounters one
    if (error) {
      console.log(error);
    }
    // Otherwise, send the result of this query to the browser
    else {
      res.json(found);
    }
  });
});

$.getJSON("/all", function(data) {
    console.log(data)
  // For each entry of that json...
  for (var i = 0; i < data.length; i++) {
    // Append each of the weapons' properties to the table
    $("#results").append("<img src = " + data[i].Picture + " />");
  }
});

推荐答案

如果使用的是mongoose,则应为集合定义一个schema,然后可以引用该架构:

If you are using mongoose you should define a schema to your collection, and than you can refer to that schema:

var weaponSchema = mongoose.Schema({
     .... your model definition here ....
});

var Weapon = mongoose.model('weapon', weaponSchema);

比你能做的

Weapon.find({})........

这篇关于TypeError:无法使用猫鼬读取未定义的属性"find"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆