#Mongoose.find always returning undefined despite all parameters matching exactly

8 messages · Page 1 of 1 (latest)

young hazel

Hi there. So I have a Discord Bot (Discord.js v14.11.0 with Node 18.17.0) for a written text roleplaying game, with players having their own characters and each character having their own inventory. I've hit a snag in the inventory though, I can create it just fine, I can add items to it just fine, I can view the inventory via command just fine, but the moment I try to use an item from the inventory, it never works out. I have the item named stored, and then when I type the command to use an item, I type the name of said item, yet the code keeps reporting that the isn't in my inventory when it is (something I'm sure not only from looking at my database, but also from using the viewinventory command which works fine). I've tried copy pasting the name directly from the database, tried parsing toLowerCase, nothing. Console logging the Mongoose.find query returns as undefined and I just don't understand why. Here's a pastebin of my useitem code:

https://pastebin.com/rYEFbtaB

And here's a pastebin of the two schemas I am using in there, as well as the subschema:

https://pastebin.com/G763ZbAa

Whenever I try the command and log it via console.log('Inventory Item:', inventoryItem);, the log comes back as Inventory Item: null, and the interaction.reply comes back as "You don't have Bandana in your inventory." (I used Bandana for my tests). Here's a screenshot of the document where Bandana is, the writing is absolutely correct and I have even copy pasted directly from the database just in case... Still nothing.
The second screenshot is of my inventory collection, just in case it helps somehow.

Can someone please help, even if just pointing me in the right direction?

wet axleBOT
  • Consider reading #how-to-get-help to improve your question!
  • Explain what exactly your issue is.
  • Post the full error stack trace, not just the top part!
  • Show your code!
  • Issue solved? Press the button!
stable heron

and this ObjectId does not have a name property

you are not getting Bandana database object when you get its ObjectId so you cant get this database object's name by items.item.name

but you can do items.item and you can try finding this ObjectId in your item list database

then getting its name

young hazel

Sorry for the late reply, was at work, and thank you for the reply. I tried your suggestion just now (removing the .name from items.item) and it came back with the following error:

Error executing useitem
CastError: Cast to ObjectId failed for value "Bandana" (type string) at path "item" for model "Inventory"
    at model.Query.exec (D:\downloads\Bot\node_modules\mongoose\lib\query.js:4921:21)
    at Query.then (D:\downloads\Bot\node_modules\mongoose\lib\query.js:5020:15)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  messageFormat: undefined,
  stringValue: '"Bandana"',
  kind: 'ObjectId',
  value: 'Bandana',
  path: 'item',
  reason: BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer
      at new BSONTypeError (D:\downloads\Bot\node_modules\mongoose\node_modules\bson\lib\error.js:41:28)
      at new ObjectId (D:\downloads\Bot\node_modules\mongoose\node_modules\bson\lib\objectid.js:67:23)
      at castObjectId (D:\downloads\Bot\node_modules\mongoose\lib\cast\objectid.js:25:12)
      at ObjectId.cast (D:\downloads\Bot\node_modules\mongoose\lib\schema\objectid.js:246:12)
      at SchemaType.applySetters (D:\downloads\Bot\node_modules\mongoose\lib\schematype.js:1201:12)
      at SchemaType._castForQuery (D:\downloads\Bot\node_modules\mongoose\lib\schematype.js:1649:15)
      at SchemaType.castForQuery (D:\downloads\Bot\node_modules\mongoose\lib\schematype.js:1637:15)
      at SchemaType.castForQueryWrapper (D:\downloads\Bot\node_modules\mongoose\lib\schematype.js:1613:20)
      at cast (D:\downloads\Bot\node_modules\mongoose\lib\cast.js:342:32)
      at Query.cast (D:\downloads\Bot\node_modules\mongoose\lib\query.js:5351:12),
  valueType: 'string'
}```