Answer the question
In order to leave comments, you need to log in
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);
var mongoose = require('mongoose');
require('./schema');
Answer the question
In order to leave comments, you need to log in
because require "executes the module" once,
when reconnecting it returns the previously loaded
mongose module apparently has internal model registration.
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 questionAsk a Question
731 491 924 answers to any question