L
L
low molecular macro2020-10-25 11:40:35
npm
low molecular macro, 2020-10-25 11:40:35

How is only the necessary one imported from the module (see inside)?

I'm new to this whole topic and don't understand the next point.
I usually import, for example, a schema from mongoose like this -> Schema = mongoose.Schema;. That is, he pointed directly.
But in one of the lessons on YouTube, I saw this way of importing: And it became interesting to me how, with such an import from mongoose, Schema is imported into the Schema variable ? And in model - exactly model ? Is this due to the fact that from the mongoose package itself they are exported to in that order? PS speaking of packages... what are all those model , schema names relative to the mongoose package itself ? Package methods?
const {Schema, model, Types} = require('mongoose')

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey delphinpro, 2020-10-25
@molekulyarniy

Is this due to the fact that from the mongoose package itself they are exported to in that order?

Of course not. This is a named export.
You can export an object from a module by default: There can be only one such export, and when importing, you can name the variable whatever you like: And there is an export by name:
export default function() {}
import anything from 'my-module';
export const CONST_1 = 1;
export function MyFunc() {}

This way you can export as many things as you like. You can import them by name . Default export and export by name can be combined in one module.
import { MyFunc, CONST_1 } from 'my-module';
import anything, { MyFunc, CONST_1 } from 'my-module';

D
Dmitry Belyaev, 2020-10-25
@bingo347

Specifically, in this example, npm and modules have nothing to do with it, the usual js features:
https://developer.mozilla.org/en-US/docs/Web/JavaS...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question