node.js Mongoose Model.find 不是一个函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34241970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mongoose Model.find is not a function?
提问by dumbname92
Spent hours trying to figure this out - I'm adding a new Model to my app but it's failing with "TypeError: List.find is not a function". I have another model, Items, that is set up in the same way and is working fine. Things seem to be failing in the route but it works if I hook it up to the Item model. Am I declaring the Schema incorrectly? Do I need to init the model in mongo or something?
花了几个小时试图解决这个问题 - 我正在向我的应用程序添加一个新模型,但它因“TypeError: List.find is not a function”而失败。我有另一个模型,Items,它以相同的方式设置并且工作正常。事情似乎在路线上失败了,但如果我将它连接到 Item 模型,它就可以工作。我是否错误地声明了架构?我需要在 mongo 中初始化模型吗?
model
模型
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var listSchema = new Schema({
name: { type: String, default: datestring + " List" }
});
mongoose.exports = mongoose.model('List', listSchema);
route
路线
app.get('/lists', function (req, res, err) {
List.find(function (err, docs){ //THIS IS WHAT'S FAILING
res.json(docs);
});
});
controller
控制器
angular.module('pickUp').controller('ListsCtrl', ['$scope', '$http', 'ngDialog', 'lists',
function($scope, $http, ngDialog, lists) {
$scope.lists = lists.lists;
}]);
factory
工厂
angular.module('pickUp').factory('lists', ['$http',
function($http){
var lists = {
lists: []
};
lists.getAll = function(){
console.log("trying. . .");
$http.get('/lists').success(function(res){
angular.copy(res, lists.lists);
});
};
return lists;
}]);
config
配置
$stateProvider
.state('/', {
url: '/',
templateUrl: 'views/lists.html',
controller: 'ListsCtrl',
resolve: {
listPromise: ['lists', function (lists){
return lists.getAll();
}]
回答by AfDev
Your module export is incorrect
您的模块导出不正确
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var listSchema = new Schema({
name: { type: String, default: datestring + " List" }
});
**mongoose.exports = mongoose.model('List', listSchema);** <!-- this is wrong -->
it should be
它应该是
**module.exports = mongoose.model('List', listSchema)**
回答by dumbname92
i faced this issue . to solve this , you need to understand one logic .
you need to call .findas promise to model which is imported from models file.
我遇到了这个问题。要解决这个问题,您需要了解一个逻辑。您需要调用.find从模型文件导入的模型作为承诺。
example:
例子:
const member = require('..// path to model')
//model initiation
const Member = new member();
exports.searchMembers = function (req,res) {
Member.find({},(err,docs)=>{
res.status(200).json(docs)
})
}
this code dont work because i called find()to initiated schema
此代码不起作用,因为我调用find()了已启动的架构
code that works :
有效的代码:
exports.searchMembers = function (req,res) {
member.find({},(err,docs)=>{
res.status(200).json(docs)
})
}
here i called .find()directly to imported model
在这里我.find()直接调用导入模型
回答by KARTHIKEYAN.A
To import model instance and call method, for example
导入模型实例和调用方法,例如
modelfile.js
模型文件.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const NotificationSchema = new Schema({
count: Number,
color: String,
icon: String,
name: String,
date: {type: Date, default: Date.now },
read: Boolean
});
module.exports = mongoose.model('notifications', NotificationSchema);
queryfile.js
查询文件.js
const Notification = require('./models/model-notifications');
function totalInsert(online) {
let query = { name: 'viewed count' };
Notification.find(query,(err,result)=>{
if(!err){
totalCount.count = online + result.length;
totalCount.save((err,result)=>{
if(!err){
io.emit('total visitor', totalCount);
}
});
}
});
}
回答by Himansh
You've defined incorrect module.exports.
您定义了不正确的 module.exports。
mongoose.exports = mongoose.model('List', listSchema);
mongoose.exports = mongoose.model('List', listSchema);
This should be
这应该是
module.exports = mongoose.model("List", listSchema);
module.exports = mongoose.model("List", listSchema);

