我打印时
让app = express()
在控制台上它像这样打印。
{ [EventEmitter: app]
_events: { mount: [Function: onmount] },
_eventsCount: 1,
_maxListeners: undefined,
setMaxListeners: [Function: setMaxListeners],
getMaxListeners: [Function: getMaxListeners],
emit: [Function: emit],
addListener: [Function: addListener],
on: [Function: addListener],
prependListener: [Function: prependListener],
once: [Function: once],
...
}
在代码片段中,有一个on:函数等...因此,我认为这使我可以像在此app上一样使用function.on(...)
const Grid = require('gridfs-stream');
let gfs;
conn.once('open', () => {
// Init stream
gfs = Grid(conn.db, mongoose.mongo);
gfs.collection('uploads');
console.log(gfs);
});
但是,当我在控制台上打印gfs对象时,它会打印出来。 这是我的示例链接:https://github.com/qkreltms/mongodb_simple_file_upload_to_db_example/blob/master/app.js
Grid {
db:
Db {
_events:
{ reconnect: [Function], close: [Function], timeout: [Function] },
_eventsCount: 3,
_maxListeners: undefined,
s:
{ databaseName: 'imgSaveTest',
dbCache: {},
children: [],
topology: [Server],
options: [Object],
logger: [Logger],
bson: BSON {},
readPreference: [ReadPreference],
bufferMaxEntries: -1,
parentDb: null,
pkFactory: undefined,
nativeParser: undefined,
promiseLibrary: [Function: Promise],
noListener: false,
readConcern: undefined },
serverConfig: [Getter],
bufferMaxEntries: [Getter],
databaseName: [Getter] },
mongo:
{ [Function]
MongoError: [Function: MongoError],
MongoNetworkError: [Function: MongoNetworkError],
Admin: [Function: Admin],
MongoClient:
{ [Function: MongoClient] super_: [Function], connect: [Circular] },
Db:
{ [Function: Db]
super_: [Function],
SYSTEM_NAMESPACE_COLLECTION: 'system.namespaces',
SYSTEM_INDEX_COLLECTION: 'system.indexes',
SYSTEM_PROFILE_COLLECTION: 'system.profile',
SYSTEM_USER_COLLECTION: 'system.users',
SYSTEM_COMMAND_COLLECTION: '$cmd',
SYSTEM_JS_COLLECTION: 'system.js' },
Collection: [Function: Collection],
Server: [Function: Server],
ReplSet: [Function: ReplSet],
Mongos: [Function: Mongos],
ReadPreference:
{ [Function: ReadPreference]
PRIMARY: 'primary',
PRIMARY_PREFERRED: 'primaryPreferred',
SECONDARY: 'secondary',
SECONDARY_PREFERRED: 'secondaryPreferred',
NEAREST: 'nearest',
isValid: [Function],
primary: [ReadPreference],
primaryPreferred: [ReadPreference],
secondary: [ReadPreference],
secondaryPreferred: [ReadPreference],
nearest: [ReadPreference] },
GridStore:
{ [Function: GridStore]
DEFAULT_ROOT_COLLECTION: 'fs',
DEFAULT_CONTENT_TYPE: 'binary/octet-stream',
IO_SEEK_SET: 0,
IO_SEEK_CUR: 1,
IO_SEEK_END: 2,
exist: [Function],
list: [Function],
read: [Function],
readlines: [Function],
unlink: [Function] },
Chunk: { [Function: Chunk] DEFAULT_CHUNK_SIZE: 261120 },
Logger:
{ [Function: Logger]
reset: [Function],
currentLogger: [Function],
setCurrentLogger: [Function],
filter: [Function],
setLevel: [Function] },
AggregationCursor:
{ [Function: AggregationCursor] super_: [Function], INIT: 0, OPEN: 1, CLOSED: 2 },
CommandCursor:
{ [Function: CommandCursor] super_: [Function], INIT: 0, OPEN: 1, CLOSED: 2 },
Cursor:
{ [Function: Cursor] super_: [Function], INIT: 0, OPEN: 1, CLOSED: 2, GET_MORE: 3 },
GridFSBucket: { [Function: GridFSBucket] super_: [Function] },
CoreServer:
{ [Function: Server]
super_: [Function],
enableServerAccounting: [Function],
disableServerAccounting: [Function],
servers: [Function] },
CoreConnection:
{ [Function: Connection]
super_: [Function],
enableConnectionAccounting: [Function],
disableConnectionAccounting: [Function],
connections: [Function] },
Binary:
{ [Function: Binary]
BUFFER_SIZE: 256,
SUBTYPE_DEFAULT: 0,
SUBTYPE_FUNCTION: 1,
SUBTYPE_BYTE_ARRAY: 2,
SUBTYPE_UUID_OLD: 3,
SUBTYPE_UUID: 4,
SUBTYPE_MD5: 5,
SUBTYPE_USER_DEFINED: 128,
Binary: [Circular] },
Code: { [Function: Code] Code: [Circular] },
Map: { [Function: Map] Map: [Circular] },
DBRef: { [Function: DBRef] DBRef: [Circular] },
Double: { [Function: Double] Double: [Circular] },
Int32: { [Function: Int32] Int32: [Circular] },
Long:
{ [Function: Long]
fromInt: [Function],
fromNumber: [Function],
fromBits: [Function],
fromString: [Function],
INT_CACHE_: [Object],
TWO_PWR_16_DBL_: 65536,
TWO_PWR_24_DBL_: 16777216,
TWO_PWR_32_DBL_: 4294967296,
TWO_PWR_31_DBL_: 2147483648,
TWO_PWR_48_DBL_: 281474976710656,
TWO_PWR_64_DBL_: 18446744073709552000,
TWO_PWR_63_DBL_: 9223372036854776000,
ZERO: [Long],
ONE: [Long],
NEG_ONE: [Long],
MAX_VALUE: [Long],
MIN_VALUE: [Long],
TWO_PWR_24_: [Long],
Long: [Circular] },
MinKey: { [Function: MinKey] MinKey: [Circular] },
MaxKey: { [Function: MaxKey] MaxKey: [Circular] },
ObjectID:
{ [Function: ObjectID]
index: 4899462,
createPk: [Function: createPk],
createFromTime: [Function: createFromTime],
createFromHexString: [Function: createFromHexString],
isValid: [Function: isValid],
ObjectID: [Circular],
ObjectId: [Circular] },
ObjectId:
{ [Function: ObjectID]
index: 4899462,
createPk: [Function: createPk],
createFromTime: [Function: createFromTime],
createFromHexString: [Function: createFromHexString],
isValid: [Function: isValid],
ObjectID: [Circular],
ObjectId: [Circular] },
Symbol: { [Function: Symbol] Symbol: [Circular] },
Timestamp:
{ [Function: Timestamp]
fromInt: [Function],
fromNumber: [Function],
fromBits: [Function],
fromString: [Function],
INT_CACHE_: [Object],
TWO_PWR_16_DBL_: 65536,
TWO_PWR_24_DBL_: 16777216,
TWO_PWR_32_DBL_: 4294967296,
TWO_PWR_31_DBL_: 2147483648,
TWO_PWR_48_DBL_: 281474976710656,
TWO_PWR_64_DBL_: 18446744073709552000,
TWO_PWR_63_DBL_: 9223372036854776000,
ZERO: [Timestamp],
ONE: [Timestamp],
NEG_ONE: [Timestamp],
MAX_VALUE: [Timestamp],
MIN_VALUE: [Timestamp],
TWO_PWR_24_: [Timestamp],
Timestamp: [Circular] },
BSONRegExp: { [Function: BSONRegExp] BSONRegExp: [Circular] },
Decimal128:
{ [Function: Decimal128] fromString: [Function], Decimal128: [Circular] },
connect: [Circular],
instrument: [Function] },
curCol: 'uploads',
_col:
Collection {
s:
{ pkFactory: [Function],
db: [Db],
topology: [Server],
dbName: 'imgSaveTest',
options: [Object],
namespace: 'imgSaveTest.uploads.files',
readPreference: [ReadPreference],
slaveOk: true,
serializeFunctions: undefined,
raw: undefined,
promoteLongs: undefined,
promoteValues: undefined,
promoteBuffers: undefined,
internalHint: null,
collectionHint: null,
name: 'uploads.files',
promiseLibrary: [Function: Promise],
readConcern: undefined,
writeConcern: undefined } } }
gfs.collection('uploads');
您可以看到其中没有收集功能,但是即使控制台上不存在它也可以使用。 所以问题是收集功能从何而来?
谢谢。
答案 0 :(得分:0)
在这种情况下
let app = express()
记录express函数,并在控制台中显示其功能。
但是在这种情况下
const Grid = require('gridfs-stream');
Grid尚未被调用,您在调用它后正在检查功能。调用GFS后尝试控制台gfs
gfs = Grid(conn.db, mongoose.mongo);
console.log(gfs)
答案 1 :(得分:0)
好吧,我现在甚至都不知道为什么控制台上没有收集功能,但这是我想要的答案。
链接:https://github.com/qkreltms/mongodb_simple_file_upload_to_db_example/blob/master/app.js
Grid.prototype.collection = function (name) {
this.curCol = name || this.curCol || this.mongo.GridStore.DEFAULT_ROOT_COLLECTION;
return this._col = this.db.collection(this.curCol + ".files");
}