K
K
Kroid2014-01-30 12:25:21
Node.js
Kroid, 2014-01-30 12:25:21

Why does the 'mongoose' variable from the 'script.js' file contain the model from the 'schema.js' file on startup?

Faced with unusual behavior for me. There are 2 files in the same folder:
schema.js

var mongoose = require('mongoose');
var bugSchema = mongoose.Schema({
  name: String,
  value: Number
});
mongoose.model('Bug', bugSchema);

script.js
var mongoose = require('mongoose');
require('./schema');

Why does the 'mongoose' variable from the 'script.js' file contain the model from the 'schema.js' file on startup?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
snackovich, 2014-01-30
@Kroid

because require "executes the module" once,
when reconnecting it returns the previously loaded
mongose ​​module apparently has internal model registration.

K
Konstantin Kitmanov, 2014-01-30
@k12th

Because mongoose.model('Bug', bugSchema);.
In the future, the scheme registered in this way can be used like this:

var mongoose = require('mongoose');
var Bug = mongoose.model('Bug');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question