【发布时间】:2016-03-18 10:54:15
【问题描述】:
花了几个小时试图解决这个问题 - 我正在向我的应用程序添加一个新模型,但它因“TypeError: List.find is not a function”而失败。我有另一个模型,项目,它以相同的方式设置并且工作正常。事情似乎在路线上失败了,但如果我将它连接到 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();
}]
【问题讨论】: