如何先在MongoDB上插入数据,包括即将创建的消息的消息id,discord.js?

编程入门 行业动态 更新时间:2024-10-06 10:26:50

如何先在MongoDB上插入数据,包括即将创建的<a href=https://www.elefans.com/category/jswz/34/1771421.html style=消息的消息id,discord.js?"/>

如何先在MongoDB上插入数据,包括即将创建的消息的消息id,discord.js?

这是我需要做的:

  1. 用户使用生成新消息的不和谐命令。
  2. 获取将要创建的消息的消息 ID。
  3. 然后用消息ID向MongoDB插入数据
  4. 将数据插入数据库后,发送新消息。

我只想在插入数据库成功后显示消息。我不确定如何获取尚未发送的新消息的 ID。这有意义吗?

这是我目前的代码:

if (command === "generatemessage") {
    db.collection("doc").insertOne({newMessageId: "", title: "This is new"}) //empty message id, my problem
    .then(() => {
        if (insert is success) {
            const msg = message.channel.send("This is the new message!");
            console.log(msg.id); //get the new msg id here
        }
    })
}

我可以这样做,但这不是我想要的,因为无论插入数据库是否成功,这都会显示消息:

if (command === "generatemessage") {
    const msg = message.channel.send("This is the new message!");
    db.collection("doc").insertOne({newMessageId: msg.id, title: "This is new"})
}
回答如下:

更多推荐

如何先在MongoDB上插入数据,包括即将创建的消息的消息id,discord.js?

本文发布于:2024-05-13 16:03:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1759990.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:消息   先在   数据   MongoDB   js

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!