这是我正在尝试做的事情:
我在 #5 上收到错误,因为客户端准备就绪并尝试使用未定义的值进行编辑,因为数据库查询尚未完成。
代码如下:
let newValue; //global variable
connectToDb((err) => { //connect to db
if (!err) { //if there is no error then execute
console.log("Database ready.");
db = getDb();
db.collection("document").findOne({id: id})
.then((result) => {
newValue = result.value; //assign value to global variable
})
}
})
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}`);
client.channels.cache.get(discordChannelId).messages.fetch(discordMessageId) //fetch previous msg
.then((msg) => {
msg.edit(newValue); //edit the previous msg with new value
})
});
我收到此错误:无法读取未定义的属性(读取“newValue”)