Mongoose Model.find 是不是一个函数? [英] Mongoose Model.find is not a function?
本文介绍了Mongoose Model.find 是不是一个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
花了几个小时试图解决这个问题 - 我正在向我的应用程序添加一个新模型,但它因TypeError:List.find 不是函数"而失败.我有另一个模型,Items,它以相同的方式设置并且工作正常.事情似乎在路线上失败了,但如果我将它连接到 Item 模型,它就可以工作.我是否错误地声明了架构?我需要在 mongo 中初始化模型吗?
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?
型号
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var listSchema = new Schema({
name: { type: String, default: datestring + " List" }
});
mongoose.exports = mongoose.model('List', listSchema);
路线
app.get('/lists', function (req, res, err) {
List.find(function (err, docs){ //THIS IS WHAT'S FAILING
res.json(docs);
});
});
控制器
angular.module('pickUp').controller('ListsCtrl', ['$scope', '$http', 'ngDialog', 'lists',
function($scope, $http, ngDialog, lists) {
$scope.lists = lists.lists;
}]);
工厂
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;
}]);
配置
$stateProvider
.state('/', {
url: '/',
templateUrl: 'views/lists.html',
controller: 'ListsCtrl',
resolve: {
listPromise: ['lists', function (lists){
return lists.getAll();
}]
推荐答案
你的模块导出不正确
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 -->
应该是
**module.exports = mongoose.model('List', listSchema)**
这篇关于Mongoose Model.find 是不是一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文