简体   繁体   English

类型错误:无法使用猫鼬读取未定义的属性“查找”

[英]TypeError: Cannot read property 'find' of undefined with mongoose

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

I have mongoose installed and everything connects so I can't figure out why it won't recognize the find property.我安装了 mongoose 并且一切都已连接,所以我无法弄清楚为什么它无法识别 find 属性。 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 + " />"); } });

If you are using mongoose you should define a schema to your collection, and than you can refer to that schema:如果您使用的是mongoose您应该为您的集合定义一个schema ,然后您可以参考该架构:

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

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

Than you can do:比你能做的:

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

For those of you who were interested I figured out the problem.对于那些感兴趣的人,我找到了问题所在。

The issue was the lack of schema which I resolved but also the collection name in mongodb needed to be a plural and not a singular (so weapons not weapon).问题是缺乏我解决的模式,而且 mongodb 中的集合名称需要是复数而不是单数(所以武器不是武器)。

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

相关问题 猫鼬:TypeError:无法读取未定义的属性“ findOne” - Mongoose: TypeError: Cannot read property 'findOne' of undefined Mongoose TypeError:无法读取undefined的属性&#39;googleID&#39; - Mongoose TypeError: Cannot read property 'googleID' of undefined Loopback:TypeError:无法读取未定义的属性“查找” - Loopback:TypeError: Cannot read property 'find' of undefined Uncaught TypeError:无法读取未定义的属性“ find” - Uncaught TypeError: Cannot read property 'find' of undefined 类型错误:无法使用 find() 和尚读取未定义的属性“then” - TypeError: Cannot read property 'then' of undefined with find() monk Javascript 类型错误:无法读取未定义的属性“查找” - Javascript TypeError: Cannot read property 'find' of undefined angularJS +猫鼬:TypeError:无法读取未定义的属性“ model” - angularJS + mongoose: TypeError: Cannot read property 'model' of undefined 更新Mongoose文档失败TypeError:无法读取未定义的属性“ coordinates” - Update Mongoose document fails TypeError: Cannot read property 'coordinates' of undefined mongoose 通过 mongo atlas TypeError:无法读取未定义的属性“长度” - mongoose through mongo atlas TypeError: Cannot read property 'length' of undefined reduce()和find()引发TypeError-无法读取未定义的属性&#39;find&#39; - reduce() & find() throw TypeError - Cannot read property 'find' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM