【问题标题】:Mongoose Model.find is not a function?Mongoose Model.find 是不是一个函数?
【发布时间】: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();
        }]

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    您的模块导出不正确

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

    【讨论】:

    • 我完全按照你的建议做,但发生了同样的错误。
    【解决方案2】:

    我遇到了这个问题。要解决这个问题,你需要了解一个逻辑。 您需要调用 .find 作为从模型文件导入的模型的承诺。

    示例:

    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()来导入模型

    【讨论】:

      【解决方案3】:

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

      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);
              }
            });
          }
        });
      }
      

      【讨论】:

        【解决方案4】:

        您定义了不正确的 module.exports。

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

        这应该是

        module.exports = mongoose.model("List", listSchema);

        【讨论】:

          【解决方案5】:

          确保您已正确导出模型/架构。

          module.exports = User
          

          在上述步骤之后,确保在 index.js(主路由文件)中需要 Model/Schema.js 文件

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

          下面是一些用于解释的sn-ps。一切顺利。! ?

          【讨论】:

            猜你喜欢
            • 2017-03-28
            • 2021-10-22
            • 1970-01-01
            • 2018-07-27
            • 2018-10-11
            • 2018-03-20
            • 2017-10-13
            • 2021-09-11
            • 1970-01-01
            相关资源
            最近更新 更多