node.js - 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"
}
}
}
解决方案
由于您的数据库字符串而发生此错误,只需从设置字符串的末尾删除“&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"
希望错误现在消失了。
如果仍然存在错误,请检查末尾的任何空格或重写您的字符串。希望这会破解。
推荐阅读
- javascript - 如何在 NodeJS 中使用 Priority 值执行 HTTP 请求?
- flutter - const 关键字用于文本和按钮填充器
- php - 我可以在 Facadesdb 插入中获取主键以直接使用 - Laravel 8
- c++ - 在 VC++ 中为 libcurl_a.lib 获取未解决的外部符号错误
- drupal-8 - 如何以编程方式仅使用自定义表单中的字母和数字验证 TextField?
- python - 部署简单的烧瓶休息 api 的最佳方法是什么
- spring - IllegalStateException: Spring Data REST controller ... 不能在类级别使用@RequestMapping,因为这会导致双重注册
- python - Heroku - Flask-Mail 超时问题
- c# - XmlReader 优化
- python - 如果请求在一定时间内没有完成怎么办