2

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.

4
  • 1
    Can you add a link to the tutorial?
    – Barmar
    Commented Aug 29, 2018 at 22:00
  • @Barmar I can't because it's on udemy.
    – Omer Ozer
    Commented Aug 29, 2018 at 22:01
  • Have you connected to the db with mongoose.connect(url)? Commented Aug 29, 2018 at 22:02
  • 1
    Yes : var mongoose = require('mongoose'); mongoose.Promise = global.Promise; mongoose.connect('mongodb://localhost:27017'); module.exports = {mongoose}; @DavidKamer
    – Omer Ozer
    Commented Aug 29, 2018 at 22:05

4 Answers 4

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(...);
2
  • Can you call .find() without an empty object? I get confused on this because you have to in Mondo shell. Commented Aug 29, 2018 at 22:14
  • 1
    In mongo shell, calling db.collection.find() with no arguments will return all documents in the collection
    – David Z.
    Commented Aug 29, 2018 at 22:18
1
app.get(URLS.USERS_URL, async (req,res)=>{

    const userList = await User.find();

    if(!userList) {
      res.status(500).json({success: false});
    }
    res.send(userList);

});
0

Your call to the database needs to look like this:

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

You should call it directly on the module, because mongoose will handle creation implicitly and creating a new object isn't neccesary.

I'm not sure if your schema is correctly defined, but I'm not going to say your tutorial is wrong on that. You should go into mongo shell and check if the schema was created to verify it was designed correctly.

0

In my case, I wrote wrong this so check your file exports module.exports = XYZ format.

PS:- I wrote like this exports.module = XYZ

Not the answer you're looking for? Browse other questions tagged or ask your own question.