首页 > 解决方案 > MongoWriteConcernError:没有名为“多数”的写入关注模式;在 MessageStream.messageHandler 的副本集配置中找到

问题描述

我正在使用 createTask 函数在 mongodb atlas 中插入文档,该文档正在保存在 atlas 中,但 catch 部分也在工作,因此插入文档后的 res.send 不起作用。

const createTask=async (req,res)=>{
    try{
      const task = await Task.create(req.body);
    //   res.status(201).json({task});
        res.send('done');
    }
    catch(err){res.status(500).json({msg:err});}
}

当我使用 catch 发送错误时,错误来了 -

"msg": {
        "code": 79,
        "codeName": "UnknownReplWriteConcern",
        "errInfo": {
            "writeConcern": {
                "w": "majority;",
                "wtimeout": 0,
                "provenance": "clientSupplied"
            }
        },
        "name": "MongoWriteConcernError",
        "result": {
            "n": 1,
            "opTime": {
                "ts": "7021011505814437896",
                "t": 192
            },
            "electionId": "7fffffff00000000000000c0",
            "ok": 1,
            "writeConcernError": {
                "code": 79,
                "codeName": "UnknownReplWriteConcern",
                "errmsg": "No write concern mode named 'majority;' found in replica set configuration",
                "errInfo": {
                    "writeConcern": {
                        "w": "majority;",
                        "wtimeout": 0,
                        "provenance": "clientSupplied"
                    }
                }
            },
            "$clusterTime": {
                "clusterTime": "7021011505814437896",
                "signature": {
                    "hash": "C1HLM9Tmi+sPwFdXIPTEATVpmuA=",
                    "keyId": "6978775665008967681"
                }
            },
            "operationTime": "7021011505814437896"
        }
    }
}

标签: node.jsmongodbatlas

解决方案


由于您的数据库字符串而发生此错误,只需从设置字符串的末尾删除“&w=majority”。

DB = "mongodb+srv://name:Password@cluster0.f60fb.mongodb.net/mernstack?retryWrites=true&w=majority"

而是写成

DB = "mongodb+srv://name:Password@cluster0.f60fb.mongodb.net/mernstack?retryWrites=true"

希望错误现在消失了。

如果仍然存在错误,请检查末尾的任何空格或重写您的字符串。希望这会破解。


推荐阅读