MongoDB 不更新信息

问题描述

我的代码有问题。
我想在发出事件时更新 MongoDB 字段,但是当我调用该函数时,什么也没有发生。

这是我的代码(客户端):

    function edit() {
      socket.emit("current_information",id)
      socket.on("res_current",function(data) {
        socket.emit("bot_modify",{
            "current_id": id,"current_name": data.name,"current_description":data.description,"current_invite":data.invite,"current_support_server":data.support_server,"current_avatar":data.avatar,"current_other_developers": data.other_developers,"name":document.getElementById("name").value,"description":document.getElementById("description").value,"invite":document.getElementById("invite").value,"id":document.getElementById("id").value,"support_server":document.getElementById("support-server").value,"avatar":document.getElementById("avatar").value,"other_developers":document.getElementById("developer").value
          })
      })
    }

这是我的代码(服务器):

    socket.on("bot_modify",function(data) {
        let collection = mongoose.db("wumpusCave").collection("bots")

        let id = data.id;
        let name = data.name;

        let description = data.description;
        let invite = data.invite;

        let support_server = data.support_server;

        let avatar = data.avatar;
        let other_developers = data.other_developers;

        if(data.id != null) {
            collection.findOneAndUpdate({"id":data.current_id},{$set:{"id":id}})
        }
        if(data.name != null) {
            collection.findOneAndUpdate({"name":data.current_name},{$set:{"name":name}})
        }
        if(data.description != null) {
            collection.findOneAndUpdate({"description":data.current_description},{$set:{"description":description}})
        }
        if(data.invite != null) {
            collection.findOneAndUpdate({"invite":data.current_invite},{$set:{"invite":invite}})
        }
        if(data.support_server != null) {
            collection.findOneAndUpdate({"support_server":data.current_support_server},{$set:{"support_server":support_server}})
        }
        if(data.avatar != null) {
            collection.findOneAndUpdate({"avatar":data.current_avatar},{$set:{"avatar":avatar}})
        }
        if(data.other_developers != null) {
            collection.findOneAndUpdate({"other_developers":data.current_other_developers},{$set:{"other_developers":other_developers}})
        }
    })

    
    socket.on("current_information",async function(data) {
        let collection = mongoose.db("wumpusCave").collection("bots")
        let bot = await collection.findOne({"id":data})
        if(bot) {
            socket.emit("res_current",{"name": bot.name,"description": bot.description,"avatar": bot.avatar,"developer": bot.developer,"invite": bot.invite,"other_developers": bot.other_developers,"support_server":bot.support_server})
        }
    })

我正在使用 Node.js、Socket.io 和 MongoDB。
我该如何解决?
感谢您的建议,对于不好的英语深表歉意。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...