猫鼬Model.find不是函数吗?

编程入门 行业动态 更新时间:2024-10-21 18:41:01
本文介绍了猫鼬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不是函数吗?

本文发布于:2023-10-30 04:16:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1541736.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   猫鼬   Model   find

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!