I have a MongoDB document, and I can access a document by doing this:
let profileData;
try {
profileData = await profileModel.findOne({ userID: message.author.id });
if (!profileData) {
let profile = await profileModel.create(aBlueprintObject);
profile.save();
}
} catch (err) {
}
(Don't mind what message
is. Just know that profileData
is a document in the collection profileModel
, and aBlueprintObject
is an default object I use to create my document.)
Now, when I do console.log(profileData)
, this gets returned:
{
_id: new ObjectId("615023dd2d00db6f8bef1ea9"),
userID: '801715615424315432',
username: 'Tyrcnex#9403',
items: [],
bank: 0,
coins: 1610
}
But when I run this code:
for (const item in profileData) {
console.log(profileData[item]);
}
It returns this monstrocity:
InternalCache {
activePaths: StateMachine {
paths: {
coins: 'init',
bank: 'init',
username: 'init',
items: 'init',
_id: 'init',
userID: 'init'
},
states: {
ignore: {},
default: {},
init: [Object],
modify: {},
require: {}
},
stateNames: [ 'require', 'modify', 'init', 'default', 'ignore' ]
},
ownerDocument: undefined,
fullPath: undefined,
emitter: EventEmitter {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: 0,
[Symbol(kCapture)]: false
},
'$options': { skipId: true, isNew: false, willInit: true, defaults: true },
strictMode: true,
selected: {},
_id: new ObjectId("615023dd2d00db6f8bef1ea9"),
saving: ParallelSaveError: Can't save() the same doc multiple times in parallel. Document: 615023dd2d00db6f8bef1ea9
at model.Model.save (/Volumes/My Files/GitHub/discordbots/fardbot/node_modules/mongoose/lib/model.js:481:23)
at module.exports (/Volumes/My Files/GitHub/discordbots/fardbot/events/guild/message.js:47:17)
at processTicksAndRejections (internal/process/task_queues.js:95:5),
'$versionError': VersionError: No matching document found for id "615023dd2d00db6f8bef1ea9" version 0 modifiedPaths ""
at generateVersionError (/Volumes/My Files/GitHub/discordbots/fardbot/node_modules/mongoose/lib/model.js:437:10)
at model.Model.save (/Volumes/My Files/GitHub/discordbots/fardbot/node_modules/mongoose/lib/model.js:493:28)
at module.exports (/Volumes/My Files/GitHub/discordbots/fardbot/events/guild/message.js:47:17)
at processTicksAndRejections (internal/process/task_queues.js:95:5) {
version: 0,
modifiedPaths: []
},
saveOptions: SaveOptions {},
validating: ParallelValidateError: Can't validate() the same doc multiple times in parallel. Document: 615023dd2d00db6f8bef1ea9
at model.Document.validate (/Volumes/My Files/GitHub/discordbots/fardbot/node_modules/mongoose/lib/document.js:2344:27)
at model.validateBeforeSave (/Volumes/My Files/GitHub/discordbots/fardbot/node_modules/mongoose/lib/plugins/validateBeforeSave.js:35:12)
at callMiddlewareFunction (/Volumes/My Files/GitHub/discordbots/fardbot/node_modules/kareem/index.js:483:23)
at next (/Volumes/My Files/GitHub/discordbots/fardbot/node_modules/kareem/index.js:58:7)
at Kareem.execPre (/Volumes/My Files/GitHub/discordbots/fardbot/node_modules/kareem/index.js:87:8)
at Kareem.wrap (/Volumes/My Files/GitHub/discordbots/fardbot/node_modules/kareem/index.js:266:8)
at model.$__save (/Volumes/My Files/GitHub/discordbots/fardbot/node_modules/kareem/index.js:376:11)
at /Volumes/My Files/GitHub/discordbots/fardbot/node_modules/mongoose/lib/model.js:506:10
at /Volumes/My Files/GitHub/discordbots/fardbot/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
at new Promise (<anonymous>)
}
false
validate
{
_id: new ObjectId("615023dd2d00db6f8bef1ea9"),
userID: '801715615424315432',
username: 'Tyrcnex#9403',
items: [],
bank: 0,
coins: 1610
}
true
<ref *1> NativeConnection {
base: Mongoose {
connections: [ [Circular *1] ],
models: { ProfileModels: Model { ProfileModels } },
events: EventEmitter {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
[Symbol(kCapture)]: false
},
options: {
pluralization: true,
autoIndex: true,
autoCreate: true,
[Symbol(mongoose:default)]: true
},
_pluralize: [Function: pluralize],
Schema: [Function: Schema] {
reserved: [Object: null prototype],
Types: [Object],
ObjectId: [Function]
},
model: [Function (anonymous)],
plugins: [ [Array], [Array], [Array], [Array], [Array] ]
},
collections: {
profilemodels: Collection {
collection: [Collection],
Promise: [Function: Promise],
modelName: 'ProfileModels',
_closed: false,
opts: [Object],
name: 'profilemodels',
collectionName: 'profilemodels',
conn: [Circular *1],
queue: [],
buffer: false,
emitter: [EventEmitter]
}
},
models: { ProfileModels: Model { ProfileModels } },
config: {},
replica: false,
options: null,
otherDbs: [],
relatedDbs: {},
states: [Object: null prototype] {
'0': 'disconnected',
'1': 'connected',
'2': 'connecting',
'3': 'disconnecting',
'99': 'uninitialized',
disconnected: 0,
connected: 1,
connecting: 2,
disconnecting: 3,
uninitialized: 99
},
_readyState: 1,
_closeCalled: undefined,
_hasOpened: true,
plugins: [],
id: 0,
_queue: [],
_listening: false,
_connectionString: 'mongodb srv://Tyrcnex:X.split(X)@fardbotdb.a4h1i.mongodb.net/botdb?retryWrites=true&w=majority',
_connectionOptions: { driverInfo: { name: 'Mongoose', version: '6.0.7' }, tls: true },
client: MongoClient {
_events: [Object: null prototype] {
topologyDescriptionChanged: [Function (anonymous)]
},
_eventsCount: 1,
_maxListeners: undefined,
s: {
url: 'mongodb srv://Tyrcnex:X.split(X)@fardbotdb.a4h1i.mongodb.net/botdb?retryWrites=true&w=majority',
sessions: Set(0) {},
bsonOptions: [Object],
namespace: [MongoDBNamespace],
options: [Getter],
readConcern: [Getter],
writeConcern: [Getter],
readPreference: [Getter],
logger: [Getter]
},
topology: Topology {
_events: [Object: null prototype],
_eventsCount: 25,
_maxListeners: undefined,
bson: [Object: null prototype],
s: [Object],
[Symbol(kCapture)]: false,
[Symbol(waitQueue)]: [Denque]
},
[Symbol(kCapture)]: false,
[Symbol(options)]: [Object: null prototype] {
hosts: [Array],
srvHost: 'fardbotdb.a4h1i.mongodb.net',
credentials: [MongoCredentials],
compressors: [Array],
connectTimeoutMS: 30000,
dbName: 'botdb',
directConnection: false,
metadata: [Object],
forceServerObjectId: false,
heartbeatFrequencyMS: 10000,
keepAlive: true,
keepAliveInitialDelay: 120000,
loadBalanced: false,
localThresholdMS: 15,
logger: [Logger],
maxIdleTimeMS: 0,
maxPoolSize: 100,
minPoolSize: 0,
minHeartbeatFrequencyMS: 500,
monitorCommands: false,
noDelay: true,
pkFactory: [Object],
raw: false,
readPreference: [ReadPreference],
retryReads: true,
retryWrites: true,
serverSelectionTimeoutMS: 30000,
socketTimeoutMS: 0,
tls: true,
writeConcern: [WriteConcern],
waitQueueTimeoutMS: 0,
zlibCompressionLevel: 0,
userSpecifiedAuthSource: false,
userSpecifiedReplicaSet: false,
replicaSet: 'atlas-rmab87-shard-0'
}
},
'$initialConnection': Promise { [Circular *1] },
db: Db {
s: {
client: [MongoClient],
options: [Object],
logger: [Logger],
readPreference: [ReadPreference],
bsonOptions: [Object],
pkFactory: [Object],
readConcern: undefined,
writeConcern: [WriteConcern],
namespace: [MongoDBNamespace]
}
},
host: 'fardbotdb-shard-00-02.a4h1i.mongodb.net',
port: 27017,
name: 'botdb'
}
undefined
801715615424315432
1610
0
Tyrcnex#9403
[]
new ObjectId("615023dd2d00db6f8bef1ea9")
undefined
615023dd2d00db6f8bef1ea9
Schema {
obj: {
userID: { type: [Function: String], require: true, unique: true },
coins: { type: [Function: Number], default: 0 },
bank: { type: [Function: Number], default: 0 },
username: { type: [Function: String], default: 'Username' },
items: { type: [Function: Array], default: [] }
},
paths: {
userID: SchemaString {
enumValues: [],
regExp: null,
path: 'userID',
instance: 'String',
validators: [],
getters: [],
setters: [],
_presplitPath: [Array],
options: [SchemaStringOptions],
_index: [Object],
[Symbol(mongoose#schemaType)]: true
},
coins: SchemaNumber {
path: 'coins',
instance: 'Number',
validators: [],
getters: [],
setters: [],
_presplitPath: [Array],
options: [SchemaNumberOptions],
_index: null,
defaultValue: 0,
[Symbol(mongoose#schemaType)]: true
},
bank: SchemaNumber {
path: 'bank',
instance: 'Number',
validators: [],
getters: [],
setters: [],
_presplitPath: [Array],
options: [SchemaNumberOptions],
_index: null,
defaultValue: 0,
[Symbol(mongoose#schemaType)]: true
},
username: SchemaString {
enumValues: [],
regExp: null,
path: 'username',
instance: 'String',
validators: [],
getters: [],
setters: [],
_presplitPath: [Array],
options: [SchemaStringOptions],
_index: null,
defaultValue: 'Username',
[Symbol(mongoose#schemaType)]: true
},
items: SchemaArray {
schemaOptions: [Object],
casterConstructor: [Function],
caster: [Mixed],
'$embeddedSchemaType': [Mixed],
'$isMongooseArray': true,
path: 'items',
instance: 'Array',
validators: [],
getters: [],
setters: [],
_presplitPath: [Array],
options: [SchemaArrayOptions],
_index: null,
defaultValue: [Function],
[Symbol(mongoose#schemaType)]: true
},
_id: ObjectId {
path: '_id',
instance: 'ObjectID',
validators: [],
getters: [],
setters: [Array],
_presplitPath: [Array],
options: [SchemaObjectIdOptions],
_index: null,
defaultValue: [Function],
[Symbol(mongoose#schemaType)]: true
},
__v: SchemaNumber {
path: '__v',
instance: 'Number',
validators: [],
getters: [],
setters: [],
_presplitPath: [Array],
options: [SchemaNumberOptions],
_index: null,
[Symbol(mongoose#schemaType)]: true
}
},
aliases: {},
subpaths: {
'items.$': Mixed {
path: 'items.$',
instance: 'Mixed',
validators: [],
getters: [],
setters: [],
_presplitPath: [Array],
options: SchemaTypeOptions {},
_index: null,
[Symbol(mongoose#schemaType)]: true,
[Symbol(mongoose:schema_mixed)]: true
}
},
virtuals: {
id: VirtualType {
path: 'id',
getters: [Array],
setters: [],
options: {}
}
},
singleNestedPaths: {},
nested: {},
inherits: {},
callQueue: [],
_indexes: [],
methods: {},
methodOptions: {},
statics: {},
tree: {
userID: { type: [Function: String], require: true, unique: true },
coins: { type: [Function: Number], default: 0 },
bank: { type: [Function: Number], default: 0 },
username: { type: [Function: String], default: 'Username' },
items: { type: [Function: Array], default: [] },
_id: { auto: true, type: 'ObjectId' },
__v: [Function: Number],
id: VirtualType {
path: 'id',
getters: [Array],
setters: [],
options: {}
}
},
query: {},
childSchemas: [],
plugins: [
{ fn: [Function (anonymous)], opts: [Object] },
{ fn: [Function (anonymous)], opts: [Object] },
{ fn: [Function], opts: [Object] },
{ fn: [Function (anonymous)], opts: [Object] },
{ fn: [Function: trackTransaction], opts: [Object] }
],
'$id': 1,
mapPaths: [],
s: { hooks: Kareem { _pres: [Map], _posts: [Map] } },
_userProvidedOptions: {},
options: {
typeKey: 'type',
id: true,
_id: true,
validateBeforeSave: true,
read: null,
shardKey: null,
discriminatorKey: '__t',
autoIndex: null,
minimize: true,
optimisticConcurrency: false,
versionKey: '__v',
capped: false,
bufferCommands: true,
strict: true,
pluralization: true
},
'$globalPluginsApplied': true,
_requiredpaths: [],
_indexedpaths: [ [ [Object], [Object] ] ]
}
Schema {
obj: {
userID: { type: [Function: String], require: true, unique: true },
coins: { type: [Function: Number], default: 0 },
bank: { type: [Function: Number], default: 0 },
username: { type: [Function: String], default: 'Username' },
items: { type: [Function: Array], default: [] }
},
paths: {
userID: SchemaString {
enumValues: [],
regExp: null,
path: 'userID',
instance: 'String',
validators: [],
getters: [],
setters: [],
_presplitPath: [Array],
options: [SchemaStringOptions],
_index: [Object],
[Symbol(mongoose#schemaType)]: true
},
coins: SchemaNumber {
path: 'coins',
instance: 'Number',
validators: [],
getters: [],
setters: [],
_presplitPath: [Array],
options: [SchemaNumberOptions],
_index: null,
defaultValue: 0,
[Symbol(mongoose#schemaType)]: true
},
bank: SchemaNumber {
path: 'bank',
instance: 'Number',
validators: [],
getters: [],
setters: [],
_presplitPath: [Array],
options: [SchemaNumberOptions],
_index: null,
defaultValue: 0,
[Symbol(mongoose#schemaType)]: true
},
username: SchemaString {
enumValues: [],
regExp: null,
path: 'username',
instance: 'String',
validators: [],
getters: [],
setters: [],
_presplitPath: [Array],
options: [SchemaStringOptions],
_index: null,
defaultValue: 'Username',
[Symbol(mongoose#schemaType)]: true
},
items: SchemaArray {
schemaOptions: [Object],
casterConstructor: [Function],
caster: [Mixed],
'$embeddedSchemaType': [Mixed],
'$isMongooseArray': true,
path: 'items',
instance: 'Array',
validators: [],
getters: [],
setters: [],
_presplitPath: [Array],
options: [SchemaArrayOptions],
_index: null,
defaultValue: [Function],
[Symbol(mongoose#schemaType)]: true
},
_id: ObjectId {
path: '_id',
instance: 'ObjectID',
validators: [],
getters: [],
setters: [Array],
_presplitPath: [Array],
options: [SchemaObjectIdOptions],
_index: null,
defaultValue: [Function],
[Symbol(mongoose#schemaType)]: true
},
__v: SchemaNumber {
path: '__v',
instance: 'Number',
validators: [],
getters: [],
setters: [],
_presplitPath: [Array],
options: [SchemaNumberOptions],
_index: null,
[Symbol(mongoose#schemaType)]: true
}
},
aliases: {},
subpaths: {
'items.$': Mixed {
path: 'items.$',
instance: 'Mixed',
validators: [],
getters: [],
setters: [],
_presplitPath: [Array],
options: SchemaTypeOptions {},
_index: null,
[Symbol(mongoose#schemaType)]: true,
[Symbol(mongoose:schema_mixed)]: true
}
},
virtuals: {
id: VirtualType {
path: 'id',
getters: [Array],
setters: [],
options: {}
}
},
singleNestedPaths: {},
nested: {},
inherits: {},
callQueue: [],
_indexes: [],
methods: {},
methodOptions: {},
statics: {},
tree: {
userID: { type: [Function: String], require: true, unique: true },
coins: { type: [Function: Number], default: 0 },
bank: { type: [Function: Number], default: 0 },
username: { type: [Function: String], default: 'Username' },
items: { type: [Function: Array], default: [] },
_id: { auto: true, type: 'ObjectId' },
__v: [Function: Number],
id: VirtualType {
path: 'id',
getters: [Array],
setters: [],
options: {}
}
},
query: {},
childSchemas: [],
plugins: [
{ fn: [Function (anonymous)], opts: [Object] },
{ fn: [Function (anonymous)], opts: [Object] },
{ fn: [Function], opts: [Object] },
{ fn: [Function (anonymous)], opts: [Object] },
{ fn: [Function: trackTransaction], opts: [Object] }
],
'$id': 1,
mapPaths: [],
s: { hooks: Kareem { _pres: [Map], _posts: [Map] } },
_userProvidedOptions: {},
options: {
typeKey: 'type',
id: true,
_id: true,
validateBeforeSave: true,
read: null,
shardKey: null,
discriminatorKey: '__t',
autoIndex: null,
minimize: true,
optimisticConcurrency: false,
versionKey: '__v',
capped: false,
bufferCommands: true,
strict: true,
pluralization: true
},
'$globalPluginsApplied': true,
_requiredpaths: [],
_indexedpaths: [ [ [Object], [Object] ] ]
}
<ref *1> Collection {
collection: Collection {
s: {
db: [Db],
options: [Object],
namespace: [MongoDBNamespace],
pkFactory: [Object],
readPreference: [ReadPreference],
bsonOptions: [Object],
readConcern: undefined,
writeConcern: [WriteConcern],
slaveOk: false
}
},
Promise: [Function: Promise],
modelName: 'ProfileModels',
_closed: false,
opts: {
autoIndex: true,
autoCreate: true,
schemaUserProvidedOptions: {},
capped: false,
Promise: [Function: Promise],
'$wasForceClosed': undefined
},
name: 'profilemodels',
collectionName: 'profilemodels',
conn: <ref *2> NativeConnection {
base: Mongoose {
connections: [Array],
models: [Object],
events: [EventEmitter],
options: [Object],
_pluralize: [Function: pluralize],
Schema: [Function],
model: [Function (anonymous)],
plugins: [Array]
},
collections: { profilemodels: [Circular *1] },
models: { ProfileModels: Model { ProfileModels } },
config: {},
replica: false,
options: null,
otherDbs: [],
relatedDbs: {},
states: [Object: null prototype] {
'0': 'disconnected',
'1': 'connected',
'2': 'connecting',
'3': 'disconnecting',
'99': 'uninitialized',
disconnected: 0,
connected: 1,
connecting: 2,
disconnecting: 3,
uninitialized: 99
},
_readyState: 1,
_closeCalled: undefined,
_hasOpened: true,
plugins: [],
id: 0,
_queue: [],
_listening: false,
_connectionString: 'mongodb srv://Tyrcnex:X.split(X)@fardbotdb.a4h1i.mongodb.net/botdb?retryWrites=true&w=majority',
_connectionOptions: { driverInfo: [Object], tls: true },
client: MongoClient {
_events: [Object: null prototype],
_eventsCount: 1,
_maxListeners: undefined,
s: [Object],
topology: [Topology],
[Symbol(kCapture)]: false,
[Symbol(options)]: [Object: null prototype]
},
'$initialConnection': Promise { [Circular *2] },
db: Db { s: [Object] },
host: 'fardbotdb-shard-00-02.a4h1i.mongodb.net',
port: 27017,
name: 'botdb'
},
queue: [],
buffer: false,
emitter: EventEmitter {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
[Symbol(kCapture)]: false
}
}
<ref *1> Collection {
collection: Collection {
s: {
db: [Db],
options: [Object],
namespace: [MongoDBNamespace],
pkFactory: [Object],
readPreference: [ReadPreference],
bsonOptions: [Object],
readConcern: undefined,
writeConcern: [WriteConcern],
slaveOk: false
}
},
Promise: [Function: Promise],
modelName: 'ProfileModels',
_closed: false,
opts: {
autoIndex: true,
autoCreate: true,
schemaUserProvidedOptions: {},
capped: false,
Promise: [Function: Promise],
'$wasForceClosed': undefined
},
name: 'profilemodels',
collectionName: 'profilemodels',
conn: <ref *2> NativeConnection {
base: Mongoose {
connections: [Array],
models: [Object],
events: [EventEmitter],
options: [Object],
_pluralize: [Function: pluralize],
Schema: [Function],
model: [Function (anonymous)],
plugins: [Array]
},
collections: { profilemodels: [Circular *1] },
models: { ProfileModels: Model { ProfileModels } },
config: {},
replica: false,
options: null,
otherDbs: [],
relatedDbs: {},
states: [Object: null prototype] {
'0': 'disconnected',
'1': 'connected',
'2': 'connecting',
'3': 'disconnecting',
'99': 'uninitialized',
disconnected: 0,
connected: 1,
connecting: 2,
disconnecting: 3,
uninitialized: 99
},
_readyState: 1,
_closeCalled: undefined,
_hasOpened: true,
plugins: [],
id: 0,
_queue: [],
_listening: false,
_connectionString: 'mongodb srv://Tyrcnex:X.split(X)@fardbotdb.a4h1i.mongodb.net/botdb?retryWrites=true&w=majority',
_connectionOptions: { driverInfo: [Object], tls: true },
client: MongoClient {
_events: [Object: null prototype],
_eventsCount: 1,
_maxListeners: undefined,
s: [Object],
topology: [Topology],
[Symbol(kCapture)]: false,
[Symbol(options)]: [Object: null prototype]
},
'$initialConnection': Promise { [Circular *2] },
db: Db { s: [Object] },
host: 'fardbotdb-shard-00-02.a4h1i.mongodb.net',
port: 27017,
name: 'botdb'
},
queue: [],
buffer: false,
emitter: EventEmitter {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
[Symbol(kCapture)]: false
}
}
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function: syncWrapper]
true
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function: increment]
[Function: _where]
[Function: remove]
[Function: remove]
[Function: remove]
[Function: deleteOne]
[Function: model]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function: update]
[Function: updateOne]
[Function: replaceOne]
[Function: $session]
[Function: overwrite]
[Function: $set]
[Function: $set]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function: isSelected]
[Function: isSelected]
[Function: isDirectSelected]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function: reset]
[Function: $__undoReset]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function: handleReject]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function: populate]
[Function: $getPopulatedDocs]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
[Function (anonymous)]
Shouldn't these two do the same thing? Does this have something to do with Prototype
?
All I wanted to do was compare the document to an object: if the document had something the object didn't have, the code would remove that property in the document. But right now I can't find a way to iterate through each item of the document, any ideas?
CodePudding user response:
It is because of how console.log
in node. You can check this answer to know more about it.