Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

node.js - Mongoose Model.find is not a function?

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?

model

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var listSchema = new Schema({
    name: { type: String, default: datestring + " List" }
});

mongoose.exports = mongoose.model('List', listSchema);

route

app.get('/lists', function (req, res, err) {
    List.find(function (err, docs){ //THIS IS WHAT'S FAILING
        res.json(docs);
    });
});

controller

angular.module('pickUp').controller('ListsCtrl', ['$scope', '$http', 'ngDialog', 'lists',

        function($scope, $http, ngDialog, lists) {

        $scope.lists = lists.lists;

    }]);

factory

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();
        }]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your module export is incorrect

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 -->

it should be

**module.exports = mongoose.model('List', listSchema)**

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...