'MongoWriteConcernError: No write concern mode named 'majority;' found in replica set configuration at MessageStream.messageHandler
I am using createTask function to insert the document in mongodb atlas , the document is getting saved in atlas but catch part is also working and hence res.send after inserting document is not working.
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});}
}
When I send the error using catch the error is coming as-
"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"
}
}
}
Solution 1:[1]
In your .env file remove &w=majoritytatus(400) from them uri to get the below
mondb_uri=mongodb+srv://name:[email protected]/mernstack?retryWrites=true
Remember to re-run whenever you edit .env files
Solution 2:[2]
This Error occurs beacuse of your database string, just remove "&w=majority" from the end of setup string.
DB = "mongodb+srv://name:[email protected]/mernstack?retryWrites=true&w=majority"
instead write as
DB = "mongodb+srv://name:[email protected]/mernstack?retryWrites=true"
hopefully error is gone now.
if still error exists, then check any spaces at the end or rewrite your string. hopefully this will crack.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | amaster |
| Solution 2 | saud00 |
