In a tutorial I saw a code like this:
var session = require('express-session');
var FileStore = require('session-file-store')(session);
And another code like this:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
But I think I can write the first code like this:
var sessionFileStore = require('session-file-store');
var FileStore = sessionFileStore.session;
And the second one like:
var Schema = require('mongoose')(Schema);
OR
var Schema = require('mongoose').Schema;
I just wanted to ask for sure, are these two kind of writing equal or there are some differences?
I also like to know what is the meaning/difference if I use something like below for the second command:
var Schema = require('mongoose')('Schema');
It is the same. require
is a normal function, which returns a value. So, if that value is also a function, you can immediately call it, or use one of its properties if it is an object.
However, in the second case, require('mongoose')(Schema)
will cause two errors:
require
returns an object, not a function, so you can't invoke it.Schema
which has not been defined yet, and passing it as an argument to a function.In the second case, the right way to put it in a single line is var Schema = require('mongoose').Schema;
And in the first case, I think that the correct way is var FileStore = sessionFileStore(session);
I assume here session
is a global variable, or has been defined previously.