简体   繁体   English

无法查询猫鼬的模型

[英]unable to query a model on mongoose

So I'm messing around with mongoose express trying to implement the MVC pattern. 因此,我在尝试实现MVC模式的猫鼬Express搞砸了。 I'm trying to basically print out my documents in the kittens collection but absolutely have no idea how to. 我正在尝试基本上将我的文档打印在小猫收藏中,但是绝对不知道该怎么做。 I've followed countless tutorials and something keeps on going wrong. 我遵循了无数的教程,并且某些地方不断出错。 Most of the tutorials I look at just shows them putting the code in one single file which is not ideal. 我看过的大多数教程只是向他们展示了将代码放在一个单独的文件中,这并不理想。 My routes do work as I've tested them with my views (I haven't included them). 我的路线可以正常运行,因为我已经用视图对它们进行了测试(未包括在内)。 The error I am getting when I am accessing /indexoflistcontrollersget is a referenceError on kitty (line 10 in listControllers.js). 当我访问/ indexoflistcontrollersget时遇到的错误是kitty上的referenceError(listControllers.js中的第10行)。 Apparently kitty is not defined but I'm sure it should be since that file requires homemodel.js hence it should already have ran that code? 显然未定义kitty,但我确定应该是因为该文件需要homemodel.js,因此它应该已经运行了该代码?

These are my files: 这些是我的文件:

app.js app.js

var express = require('express');
var http = require('http');
var path = require('path');
var handlebars  = require('express-handlebars'), hbs;
var app = express();
var db = require('./db')

require('./router')(app);

app.set('port', 1337);
app.set('views', path.join(__dirname, 'views'));

/* express-handlebars - https://github.com/ericf/express-handlebars
A Handlebars view engine for Express. */
hbs = handlebars.create({
   defaultLayout: 'main'
});

app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');

app.use(express.static(path.join(__dirname, 'static')));

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

// Connect to Mongo on start
db.connectDB();

db.js db.js

var mongoose = require('mongoose');
exports.connectDB = function() {
mongoose.connect('mongodb://localhost:27017/test');
console.log("CONNECTED TO MONDO")
}

model/homemodel.js 型号/homemodel.js

var mongoose = require('mongoose');

var Schema = mongoose.Schema;

var kittySchema = new Schema({
    name: String
});

mongoose.model('kitty', kittySchema);

controller/listControllers.js controller / listControllers.js

var express = require('express');
var router = express.Router();
var modelList = require('../model/homemodel.js');
var mongoose = require('mongoose');
exports.IndexOfListControllerGET = function(request, response){
//This is another controllers
//GET METHOD

  mongoose.model('kitty').find(function(err,kitty){
response.send(kitty);

  });


}

Please help, been stuck at this very simple problem for a long time now. 请帮助,长期困扰这个非常简单的问题。

EDIT = So I fixed the error. 编辑=所以我解决了错误。 was passing the wrong parameters. 传递了错误的参数。 But only [] is printing out. 但只有[]正在打印。 When I access the kittens collections using the mongo terminal, I can see the entries I've already put in. How can I display those entries on the page? 当我使用mongo终端访问小猫收藏时,我可以看到已经放入的条目。如何在页面上显示这些条目?

first i guess in "homemodel.js" you need a module.exports : 首先,我猜在“ homemodel.js”中,您需要一个module.exports:

    module.exports = mongoose.model('kitty', kittySchema);

try this in "controller/listControllers.js": 在“ controller / listControllers.js”中尝试一下:

var express = require('express');
var router    = express.Router();
var modelList = require('../model/homemodel.js');    

var IndexOfListControllerGET = function (request, response) {

var query = { name: 'pichou' };

modelList.find(query, function (err, kitty) {
    if (err) throw err;

    return response.send(kitty);
});
}

module.exports = IndexOfListControllerGET;

EDIT : 'Model.find' need a condition, for example you search the name of the kitty 'pichou', you can send the object '{ name: 'pichou' }', or list all results by sending empty object '{}' 编辑: 'Model.find'需要一个条件,例如,您搜索小猫'pichou'的名称,可以发送对象'{name:'pichou'}',或通过发送空对象'{}列出所有结果'

To send back all of the records about "kitty"s, try this code: 要发回有关“小猫”的所有记录,请尝试以下代码:

modelList.find({}, function (err, kittyRecords){
  if (err) throw new Error("Error in finding kittys : " + err);
  console.log("Kittys: ", kittyRecords);
  return response.send(kittyRecords);
});

The empty object passed to the mongoose model will match all the records it encounters. 传递给猫鼬模型的空对象将匹配它遇到的所有记录。 The console.log will let you see if the records match what is going to the webpage. console.log将让您查看记录是否与网页内容匹配。 It looks like the code from lefdilia has the answer, however will only return for the name of 'pichou'. 看起来来自lefdilia的代码有答案,但是只会返回“ pichou”的名称。 As suggested above, you should export the model to have an explicit handle on it. 如上所述,您应该导出模型以对其具有显式句柄。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM