从 5.0 版本开始,Mongo 放弃了对其 Node.js 驱动程序回调的支持,转而采用仅 Promise 的公共 API。 Mongoose 在 v7 中也放弃了回调支持,因此findOne()
和其他方法现在总是返回一个承诺。
受影响方法的完整列表可以是在这里找到 https://mongoosejs.com/docs/migrating_to_7.html#dropped-callback-support.
您可以使用 async/await 代替:
module.exports = {
data: new SlashCommandBuilder().setName('dbtest').setDescription('db test'),
async execute(interaction) {
try {
const data = await testSchema.findOne({
GuildID: interaction.guild.id,
UserID: interaction.user.id,
});
if (!data) {
testSchema.create({
GuildID: interaction.guild.id,
UserID: interaction.user.id,
});
}
if (data) {
console.log(data);
}
} catch (error) {
console.log(error);
}
},
};
或者只是好老then
s:
module.exports = {
data: new SlashCommandBuilder().setName('dbtest').setDescription('db test'),
execute(interaction) {
testSchema
.findOne({
GuildID: interaction.guild.id,
UserID: interaction.user.id,
})
.then((data) => {
if (!data) {
testSchema.create({
GuildID: interaction.guild.id,
UserID: interaction.user.id,
});
}
if (data) {
console.log(data);
}
})
.catch((err) => console.log(err));
},
};