Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
144
rated 0 times [  149] [ 5]  / answers: 1 / hits: 21912  / 6 Years ago, wed, august 29, 2018, 12:00:00

I'm developing a node js rest server and having an issue with my Schema queries. When I hit my end points I get the error TypeError: user.find is not a function



The following is my user.js file



var {mongoose} = require('../../dbcore/mongoose');
var Schema = mongoose.Schema;

module.exports = mongoose.model('User',new Schema( {

basicId: Schema.ObjectId,

activePurchaseIDs: {
type: [Schema.ObjectId],
default: []
},

activeOrderIDs: {
type: [Schema.ObjectId],
default: []
},

paymentOptionIDs: {
type: [Schema.ObjectId],
default: []
},

addressIDs: {
type: [Schema.ObjectId],
default: []
},

interestIDs: {
type: [Schema.ObjectId],
default: []
}

}));


and this is where it's imported/required.



var URLS = require('./urls');
var User = require('../schemas/user/user');

function init(app,mongoose) {

app.get(URLS.USERS_URL,(req,res)=>{

var user = new User({});

user.find().then((users)=>{
res.send({users});
},(err)=>{
res.status(400).send(err);
});


});



}

module.exports = init;


I was following a tutorial while writing this code and I was expecting it to work as I followed the tutorial step by step.


More From » node.js

 Answers
3

When you call var user = new User({}) you are creating a new MongoDB document based on the User model and assigning it to var user.



A single user document does not have a find() function, but your User model does.



var user = new User({});
User.find().then(...);

[#53608] Monday, August 27, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leonardok

Total Points: 114
Total Questions: 94
Total Answers: 103

Location: France
Member since Thu, Oct 27, 2022
2 Years ago
;