Issue
I can't seem to be able to pass in options when updating data in mongoose, I just get an error saying Expected 0-2 arguments, but got 3.ts(2554)
The code I'm using is almost identical to the docs. I am using the latest version (8.0.3) of mongoose.
const guild = await guildSchema.findOne({ id: interaction.guildId })
await guild?.updateOne(
{
id: interaction.user.id
},
{
$set: {
"some.value": true
}
},
{
arrayFilters: [
{
"i.recipe": "Fried rice",
"i.item": { $not: { $regex: "oil" } },
}
]
}
)
What makes me so frustrated is that acording to the docs (https://mongoosejs.com/docs/api/query.html#Query.prototype.updateOne()) this code is fine, but it' still throwing errors.
This method should even take 4 arguments according to the docs:
Parameters:
[filter] «Object»
[update] «Object|Array» the update command
[options] «Object»
[options.multipleCastError] «Boolean» by default, mongoose only returns the first error that occurred in casting the query. Turn on this option to aggregate all the cast errors.
[options.strict] «Boolean|String» overwrites the schemas strict mode option
[options.upsert=false] «Boolean» if true, and no documents found, insert a new document
[options.writeConcern=null] «Object» sets the write concern for replica sets. Overrides the schema-level write concern
[options.timestamps=null] «Boolean» If set to false and schema-level timestamps are enabled, skip timestamps for this update. Note that this allows you to overwrite timestamps. Does nothing if schema-level timestamps are not set.
[options.translateAliases=null] «Boolean» If set to true, translates any schema-defined aliases in filter, projection, update, and distinct. Throws an error if there are any conflicts where both alias and raw property are defined on the same object.
[callback] «Function» params are (error, writeOpResult)
Solution
There is a difference between the Model
, Document
and Query
classes in mongoose. However, the Model
class is a subclass of the Document
class.
You think you are using the Query.updateOne()
which takes 3 parameters and a callback.
You are actually using the Document.updateOne()
which only takes 2 parameter and a callback.
To illustrate:
const guild = await guildSchema.findOne({ id: interaction.guildId })
// guild instanceof mongoose.Model; // true
// guild instanceof mongoose.Document; // true
// guild is now a instance of the Document class because you have executed the query
// and assigned the return value from the promise to a variable
await guild?.updateOne();
// This is now Document.updateOne()
I think you should just use Model.findOneAndUpdate()
where Guild
is an instance of a Model
created using const Guild = mongoose.model('Guild', guildSchema);
const guild = await Guild.findOneAndUpdate(
{
id: interaction.user.id
},
{
$set: {
"some.value": true
}
},
{
arrayFilters: [
{
"i.recipe": "Fried rice",
"i.item": { $not: { $regex: "oil" } },
}
],
new: true
}
)
Answered By - jQueeny
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.