0
My model works as follows. A user has documents, but the user registers before they have these documents. What I’m basically trying to do is to update users in the document array. SCHEMA
'user strict';
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const schema = new Schema({
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
name: {
type: String,
required: true
},
documents: [
{
title: {
type: String
},
content: {
type: String
},
notes: [
{
note: {
type: String
}
}
]
}
]
});
module.exports = mongoose.model('User', schema);
So I looked for some possible answers and I came to this QUERY
exports.put = (req, res, next) => {
const id = req.params.id;
User
.update({id}, {
$push: {
"documents.$.title": req.body.title,
"documents.$.content": req.body.content
}
}).then(x => {
res.status(200).send({
message: 'Usuário atualizado com sucesso!'
});
}).catch(e => {
res.status(400).send({
message: 'Falha ao atualizar o Usuário =(', data: e
});
});
};
It returns no error, drops into THEN and gives as if the user had been updated successfully. Could someone help me?
Thank you so much for the attention and the tips. Thanks to your answer I managed to solve and I am organizing the code better. Over the Notes really was an array, it was not for an array of objects, thank you for noticing.
– bellrodrigs
Good, I’m glad you helped. =]
– Gleidson Henrique