Users.find is not a function

I’ve got this problem since your last update.

Expected behavior

I’ve created a new project called lereacteur-team. I’ve created a custom route in ./api/users.js where I have a simple route in order to get users:

const express = require("express");
const router = express.Router();
const users = require("../models/users");

router.get(
  "/api/users", async (req, res) => {
    try {
      const Users = await users.find();
      res.status(200).json(Users);
    } catch (e) {
      console.log(e);
      return "Error";
    }
  },
);
module.exports = router;

Actual behavior

I’ve got this error: TypeError: users.find is not a function when I request my route.

Here is my users model:

module.exports = (mongoose, Mongoose) => {
  const schema = Mongoose.Schema(
    {
      firstname: String,
      lastname: String,
    },
    {
      timestamps: false,
    },
  );

  return mongoose.model("users", schema, "users");
};

Setup

    "body-parser": "1.19.0",
    "chalk": "~1.1.3",
    "cookie-parser": "1.4.4",
    "cors": "2.8.5",
    "debug": "~4.0.1",
    "dotenv": "~6.1.0",
    "express": "~4.17.1",
    "express-jwt": "5.3.1",
    "forest-express-mongoose": "^7.0.1",
    "mongoose": "~5.8.2",
    "morgan": "1.9.1",
    "require-all": "^3.0.0"

Hi @XavierColombel,

I just looked at our migration documentation, and it appear that the note only mention forest-express-sequelize.

If you’ve upgraded to v7 using this note, you should be able to access your users model via

const { users } = require('../models');

Let me know if that helps :pray:

EDIT: I answered a bit fast. It seems like an issue on the lumber generation of the models/index.js.

You should be able to fix it by replacing (in models/index.js)
db[model.name] = model;
to
db[model.modelName] = model;

We are working on the fix, sorry for the inconvenience.

Ok, it’s working. Waiting for the official fix :wink:

2 Likes

The fix is available in the 4.0.1 version of lumber.

npm install -g lumber-cli@latest -s, and you should be set.

Again, sorry for the incovenience and thanks for the report :raised_hands:

1 Like