Reputation: 265
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?
model
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var listSchema = new Schema({
name: { type: String, default: datestring + " List" }
});
mongoose.exports = mongoose.model('List', listSchema);
route
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;
}]);
factory
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();
}]
Upvotes: 24
Views: 50226
Reputation: 19
Make sure that you have exported your Model/Schema correctly.
module.exports = User
After this above step make sure to require the Model/Schema.js file in your index.js(Main Route File)
const Task = require('./models/task')
Below are some snippets for the explanation. All the best.! 🙂
Upvotes: 0
Reputation: 978
You've defined incorrect module.exports.
mongoose.exports = mongoose.model('List', listSchema);
This should be
module.exports = mongoose.model("List", listSchema);
Upvotes: 1
Reputation: 20158
To import model instance and call method, for example
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);
}
});
}
});
}
Upvotes: 0
Reputation:
i faced this issue . to solve this , you need to understand one logic .
you need to call .find
as promise to model which is imported from models file.
example:
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)
})
}
this code dont work because i called find()
to initiated schema
code that works :
exports.searchMembers = function (req,res) {
member.find({},(err,docs)=>{
res.status(200).json(docs)
})
}
here i called .find()
directly to imported model
Upvotes: 10
Reputation: 1242
Your module export is incorrect
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 -->
it should be
**module.exports = mongoose.model('List', listSchema)**
Upvotes: 44