Mongoose Model.find不是函数?

时间:2015-12-12 16:07:29

标签: node.js mongodb mongoose

花了好几个小时试图解决这个问题 - 我正在为我的应用添加一个新模型,但它失败了“TypeError:List.find不是一个函数”。我有另一个模型,项目,以相同的方式设置,并正常工作。事情似乎在路线上失败但是如果我把它连接到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();
        }]

5 个答案:

答案 0 :(得分:29)

您的模块导出不正确

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

答案 1 :(得分:3)

我遇到了这个问题。要解决这个问题,你需要了解一个逻辑。 您需要将.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()称为导入的模型

答案 2 :(得分: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);
        }
      });
    }
  });
}

答案 3 :(得分:0)

您定义了错误的module.exports。

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

应该是

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

答案 4 :(得分:0)

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

~ $ curl -I http://www.apple.com
HTTP/1.1 301 Moved Permanently
Server: AkamaiGHost
Content-Length: 0
Location: https://www.apple.com/
Cache-Control: max-age=0
Expires: Mon, 14 Sep 2020 18:33:23 GMT
Date: Mon, 14 Sep 2020 18:33:23 GMT
Connection: keep-alive
strict-transport-security: max-age=31536000
Set-Cookie: geo=FI; path=/; domain=.apple.com
Set-Cookie: ccl=izHQtSPVGso4jrdTGqyAkA==; path=/; domain=.apple.com

完成上述步骤后,请确保在您的index.js(主路径文件)中需要Model / Schema.js文件

module.exports = User

下面是一些说明片段。祝一切顺利。! ?

enter image description here

enter image description here