所以我試圖從 mongodb 中檢索一個檔案,但是后來出現了這個錯誤,我不知道是它的資料庫錯誤還是代碼錯誤,所以有什么幫助嗎?
Encountered error on event listener "guildMemberAdd" for event "guildMemberAdd" at path "D:\Project\Troll Kick Bot\bin\listeners\guildMemberAdd.js" TypeError: Cannot read properties of null (reading '_id')
at GuildMemberAddListener.<anonymous> (D:\Project\Troll Kick Bot\bin\listeners\guildMemberAdd.js:34:36)
at Generator.next (<anonymous>)
at fulfilled (D:\Project\Troll Kick Bot\bin\listeners\guildMemberAdd.js:9:58)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
guildMemberAdd.ts
import { Command } from "@sapphire/framework";
import { Message } from "discord.js";
import mongoose from "mongoose";
import { victim } from "../schema/victim";
export class Victim extends Command {
public constructor(context: Command.Context, options: Command.Options) {
super(context, {
...options,
name: 'victim',
description: 'kicks the victim without mercy'
})
};
public async messageRun(message: Message) {
await mongoose.connect(process.env.Mongoose!);
const victimUser = message.mentions.members!.first();
const find = await victim.findOne({ _id: victimUser?.id});
if (find) {
return message.reply('brother that guy is already in our hitlist')
};
try {
await new victim({
_id: victimUser?.id,
guildId: message.guild?.id
}).save();
} catch(e) {
console.error(e)
} finally {
mongoose.connection.close()
}
}
}
uj5u.com熱心網友回復:
看起來這行代碼await victim.findOne({ _id: victimUser?.id});給victimUser?.id了你一個null價值。然后貓鼬似乎很恐慌地說 - 你要我找到一個_id為空的檔案。因此,錯誤更多的是 - 為什么你會得到一個nullforvictimUser?.id。檢查傳遞給函式的資料是否首先messageRun具有該欄位。id
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/462406.html
標籤:javascript 节点.js mongodb 猫鼬 不和谐.js
