Mongoose Model.find不是一个函数?

24

我花了几个小时尝试解决这个问题——我正在将一个新的模型添加到我的应用程序中,但出现了“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();
        }]
5个回答

44

你的模块导出不正确

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

4
我按照你建议的做了,但是仍然出现了相同的错误。 - Ketav

10
我遇到了这个问题。为了解决它,你需要理解一个逻辑。 你需要将.find作为Promise调用到从models文件导入的模型中。
例如:
const member = require('..// path to model')

//model initiation
const Member = new member();

exports.searchMembers = function (req,res) {


Member.find({},(err,docs)=>{
    res.status(200).json(docs)
})

}

这段代码无法工作,因为我调用了find()来初始化模式

可行的代码:

exports.searchMembers = function (req,res) {


member.find({},(err,docs)=>{
    res.status(200).json(docs)
})

}

在这里,我直接调用了导入模块的.find()方法


1
您定义的module.exports不正确。
mongoose.exports = mongoose.model('List', listSchema);
应该改为
module.exports = mongoose.model("List", listSchema);

0

导入模型实例并调用方法,例如

modelfile.js

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

const NotificationSchema = new Schema({
    count: Number,
    color: String,
    icon: String,
    name: String,
    date: {type: Date, default: Date.now },
    read: Boolean
});

module.exports = mongoose.model('notifications', NotificationSchema);

queryfile.js

const Notification = require('./models/model-notifications');

function totalInsert(online) {
  let query = { name: 'viewed count' };
  Notification.find(query,(err,result)=>{
    if(!err){
      totalCount.count = online + result.length;
      totalCount.save((err,result)=>{
        if(!err){
          io.emit('total visitor', totalCount);
        }
      });
    }
  });
}

0

请确保您已正确导出模型/模式。

module.exports = User

在进行上述步骤后,请确保在您的index.js(主路由文件)中要求Model/Schema.js文件

const Task = require('./models/task')

以下是一些示例以进行说明。祝一切顺利!

enter image description here

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接