I don't quite understand why my requests are not added to my DB. I have schema with nested objects. So I try to send requst to specific object inside of an object. Result says scucces, however nothing is added.
Here's schema:
const personSchema = new mongoose.Schema({
connections: {
parents: {type : Array},
children: {type : Array}
}
})
Here's router:
router.patch('/v2/:id', getPerson, async (req, res) => {
if (req.body.connections != null) {
if (req.body.connections.parents != null) { res.person.connections.parents.push(req.body.connections.parents); }
if (req.body.connections.children != null) { res.person.connections.children.push(req.body.connections.children); }
}
try {
const updatePerson = await res.person.save();
res.status(200).json({ message: 'Success' })
} catch (error) {
res.status(400).json({ message: error.message })
}
})
Here's middelware:
async function getPerson(req, res, next) {
let person;
try {
person = await Person.findById(req.params.id);
if (person === null) {
return res.status(404).json({ message: 'Cannot find the person' });
}
} catch (error) {
return res.status(500).json({ message: error.message });
}
res.person = person;
next();
}
Here's request:
PATCH http://localhost:3100/api-db/v2/62e28682cecc9120c7af9de5
Content-Type: application/json
{
"connections.parents" : "test"
}
Connection is established, document in db is alredy created.
It seems to me that I might doing wrong requst. I couldn't find information about nested requsts though.
What seems to be the problem?
P.S.
Other requsts that are not nested are satisfied...
I just missed some syntax:
{
"connections" : {"parents" : "test"}
}