猫鼬Model.find不是函数吗? [英] Mongoose Model.find is not a function?

查看:69
本文介绍了猫鼬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;
}]);

config

$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)**

这篇关于猫鼬Model.find不是函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆