简体   繁体   中英

MissingSchemaError: Schema hasn't been registered for model “Marker”

I am working on a project with Javascript, Node,CSS,HTML and Google Maps. I recently got this error => MissingSchemaError: Schema hasn't been registered for model "Marker". I ve tried everything I could to solve it but nothing seems to work.

This is the model for my markers:

const mongoose = require("mongoose");
const mongo = require("../connectors/mongo");
const Joi = require("Joi");

const markerSchema = new mongoose.Schema(
  {
    _id: String,
    lat: { type: Number, required: true },
    lng: { type: Number, required: true },
    date: { type: Date, default: Date.now, required: false },
    pictures: { type: [String], required: true },
    user_id: { type: String, required: true }
  },
  { timestamps: true }
);

/* Create the model from the schema. */
const Marker = mongoose.model("Marker", markerSchema);
exports.Marker = Marker;

For my routes I have a folder routes that has a folder markers and a folder for users. The folder marker has these three things: Folder users is similar. handlers.js:

const Marker = require("mongoose").model("Marker");

const find = (req, res) => {
  ...
};

...
module.exports = {
  create,
  deletion,
  find,
  findById,
  update
};

index.js:

const checkAuth = require("../../middlewares/check-auth.js");
const handlers = require("./handlers");
const validators = require("./validators");

module.exports = router => {
  router.get("/markers", checkAuth, validators.find, handlers.find);
  router.post("/markers", checkAuth, validators.create, handlers.create);
  router.get("/markers/:id", checkAuth, validators.find, handlers.findById);
  router.put("/markers/:id", checkAuth, validators.update, handlers.update);
  router.delete(
    "/markers/:id",
    checkAuth,
    validators.deletion,
    handlers.deletion
  );
  return router;
};

and validators.js

const { celebrate, Joi } = require("celebrate");

const find = celebrate({
  ...
});

...

module.exports = {
  find,
  findOne,
  create,
  update,
  deletion
};

The folder routes also has an index.js with this:

const markers = require("./markers");
const users = require("./users");

const resourceRoutes = [markers, users];

module.exports = router => {
  resourceRoutes.forEach(routes => routes(router));
  return router;
};

Any suggestions on what could be causing the problem?

In the last line on your model file, change to :

module.exports = Marker

Then at the beginning of your logic file, instead of const Marker = require("mongoose").model("Marker"); Just require the model file, like:

const Marker = require(PATH_TO_THE_MARKER_MODEL_FILE);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM