Mongoose Model.find不是一个函数?
花了好几个小时试图解决这个问题 - 我正在为我的应用添加一个新模型,但是它失败了“TypeError:List.find不是一个函数”。我有另一个模型,项目,以相同的方式设置,并正常工作。事情似乎在路线上失败但是如果我把它连接到Item模型就可以了。我是否错误地声明了架构?我是否需要在mongo或其他东西中启动模型?
模型
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)**