The below code is updating the database but is not returning the new document despite specifically stating 'returnNewDocument' to be true. I am not using mongoose but instead, I'm am using the MongoDB drivers as found here https://docs.mongodb.com/manual/reference/method/db.collection.findOneAndUpdate/
const { MongoClient } = require("mongodb");
...
const mongo = new MongoClient(process.env.MONGODB_CONNECTION_URI, {
useUnifiedTopology: true
});
if (mongo) {
mongo.connect();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "build")));
app.listen(8080, () => console.log("Server running on 8080"));
}
app.get("/v1/editprofile", function(req, res) {
const { givenName, familyName, mobile, address, serviceRadius, language } = req.query;
if (req.cookies && req.cookies.participant) {
jwt.verify(req.cookies.participant, privateKey, { algorithms: ["RS256"] }, function(err, decoded) {
if (err) res.status(400).send({ status: "logout", description: "Bad Cookie found" });
async function insertProfile() {
const id = decoded.id;
const data = await mongo
.db("Users")
.collection("participants")
.findOneAndUpdate(
{ id },
{
$set: {
givenName,
familyName,
mobile,
address,
serviceRadius,
language
}
},
{
upsert: true,
returnNewDocument: true
}
);
if (data) {
const participant = data.value
console.log(participant)
res.status(200).send({ participant });
}
}
insertProfile().catch(console.error);
});
} else {
res.status(400).send({ status: "logout", description: "No Cookie found" });
}
});