gpt4 book ai didi

node.js - Mongoose Model.find 是不是一个函数?

转载 作者:IT老高 更新时间:2023-10-28 13:09:03 25 4
gpt4 key购买 nike

花了几个小时试图解决这个问题 - 我正在向我的应用程序添加一个新模型,但它以“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);
});
});

Controller

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

最佳答案

您的模块导出不正确

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

关于node.js - Mongoose Model.find 是不是一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34241970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com