David Kennell
David Kennell

Reputation: 1227

Using mongoose.model without schema param

I'm working through this Node ToDoList App API tutorial. It has one model, one controller and one routes file: https://www.codementor.io/olatundegaruba/nodejs-restful-apis-in-10-minutes-q0sgsfhbd

Repo: https://github.com/generalgmt/RESTfulAPITutorial

In the model, we use mongoose to define TaskSchema and export mongoose.model('Tasks', TaskSchema);

In the controller, we create a Task var, set equal to mongoose.model('Tasks', TaskSchema); and use it to define several controller methods.

The server.js requires Task from the model, but never seems to use it for anything. The server also requires the routes file, which in turn require the controller, but I can'a see how they ever interact with the model.

How does the rest of the app know about the model? How does the controller know the schema for Task? Is this all mongoose magic?

Upvotes: 0

Views: 300

Answers (1)

The Task schema is being called in the controller in line #4 https://github.com/generalgmt/RESTfulAPITutorial/blob/master/api/controllers/todoListController.js#L4

It does seem like the model being required in server.js is not used.

Server.js or routes don't need to interact with the schema, as all the methods required to interact with the schema are required in the Task constructor. The controller knows about the Task schema because it is being required in the controller.

Upvotes: 1

Related Questions