I’ve been stuck on my URL Shortener project.
I’m trying to save the URLs. A document is created when user posts the url.
I console.log the document, and it looks fine. It follows the schema I created, with an extra _id added to it.
I save the url document to the database, then I use findOne() to see if it’s there. Here’s what I get when I try to console.log the result:
Query {
_mongooseOptions: {},
mongooseCollection:
NativeCollection {
collection: null,
opts:
{ bufferCommands: true,
capped: false,
'$wasForceClosed': undefined },
name: 'urls',
collectionName: 'urls',
conn:
NativeConnection {
base: [Mongoose],
collections: [Object],
models: [Object],
config: [Object],
replica: false,
hosts: null,
host: null,
port: null,
user: null,
pass: null,
name: null,
options: null,
otherDbs: [],
states: [Object],
_readyState: 0,
_closeCalled: false,
_hasOpened: false,
_listening: false },
queue: [ [Array] ],
buffer: true,
emitter:
EventEmitter {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined } },
model:
{ [Function: model]
hooks: Kareem { _pres: {}, _posts: {} },
base:
Mongoose {
connections: [Array],
models: [Object],
modelSchemas: [Object],
options: [Object],
plugins: [Array] },
modelName: 'Url',
model: [Function: model],
db:
NativeConnection {
base: [Mongoose],
collections: [Object],
models: [Object],
config: [Object],
replica: false,
hosts: null,
host: null,
port: null,
user: null,
pass: null,
name: null,
options: null,
otherDbs: [],
states: [Object],
_readyState: 0,
_closeCalled: false,
_hasOpened: false,
_listening: false },
discriminators: undefined,
'$appliedMethods': true,
'$appliedHooks': true,
_events:
[Object: null prototype] { init: [Function], save: [Function] },
_eventsCount: 2,
schema:
Schema {
obj: [Object],
paths: [Object],
aliases: {},
subpaths: {},
virtuals: [Object],
singleNestedPaths: {},
nested: {},
inherits: {},
callQueue: [Array],
_indexes: [],
methods: {},
statics: {},
tree: [Object],
query: {},
childSchemas: [],
plugins: [Array],
s: [Object],
_userProvidedOptions: undefined,
options: [Object],
'$globalPluginsApplied': true,
_requiredpaths: [] },
collection:
NativeCollection {
collection: null,
opts: [Object],
name: 'urls',
collectionName: 'urls',
conn: [NativeConnection],
queue: [Array],
buffer: true,
emitter: [EventEmitter] },
Query: { [Function] base: [Query] },
'$__insertMany': [Function],
insertMany: [Function],
'$init':
Promise { emitter: [EventEmitter], emitted: [Object], ended: false } },
schema:
Schema {
obj: { url: [Object] },
paths:
{ url: [SchemaString], _id: [ObjectId], __v: [SchemaNumber] },
aliases: {},
subpaths: {},
virtuals: { id: [VirtualType] },
singleNestedPaths: {},
nested: {},
inherits: {},
callQueue: [ [Array], [Array], [Array], [Array], [Array], [Array] ],
_indexes: [],
methods: {},
statics: {},
tree:
{ url: [Object],
_id: [Object],
__v: [Function: Number],
id: [VirtualType] },
query: {},
childSchemas: [],
plugins: [ [Object], [Object], [Object], [Object] ],
s: { hooks: [Kareem], kareemHooks: [Object] },
_userProvidedOptions: undefined,
options:
{ retainKeyOrder: false,
typeKey: 'type',
id: true,
noVirtualId: false,
_id: true,
noId: false,
validateBeforeSave: true,
read: null,
shardKey: null,
autoIndex: null,
minimize: true,
discriminatorKey: '__t',
versionKey: '__v',
capped: false,
bufferCommands: true,
strict: true,
pluralization: true },
'$globalPluginsApplied': true,
_requiredpaths: [] },
op: 'findOne',
options: { retainKeyOrder: false },
_conditions: { url: 'https://www.freecodecamp.org' },
_fields: undefined,
_update: undefined,
_path: undefined,
_distinct: undefined,
_collection:
NodeCollection {
collection:
NativeCollection {
collection: null,
opts: [Object],
name: 'urls',
collectionName: 'urls',
conn: [NativeConnection],
queue: [Array],
buffer: true,
emitter: [EventEmitter] },
collectionName: 'urls' },
_traceFunction: undefined,
_count: [Function],
_execUpdate: [Function],
_find: [Function],
_findOne: [Function],
_findOneAndRemove: [Function],
_findOneAndUpdate: [Function],
_replaceOne: [Function],
_updateMany: [Function],
_updateOne: [Function],
_error: null }
What the heck is all that? All I want is to find the url I saved and to look up its id. How do I test to see if something is successfully saved into the database? The challenges only tell us to ‘find’ or ‘find and update’ documents. How do we actually access the information returned by the database, given that the above is what we get back?