我花了几个小时尝试解决这个问题——我正在将一个新的模型添加到我的应用程序中,但出现了“TypeError: List.find is not a function”的错误。我有另一个模型Items,它以相同的方式设置,并且可以正常工作。似乎在路由中出现了问题,但如果我将其连接到Item模型,则可以正常工作。我是否声明Schema不正确?我需要在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();
}]