I'm currently using these two Schema in my MongoDB:
Child Schema
const dataStructureSchema = new mongoose.Schema({
id: String,
type: String,
content: String
});
let DataStructureModel = mongoose.model("DataStructureCollection", dataStructureSchema);
Parent Schema
const elementSchema = new mongoose.Schema({
id: String,
level: Number,
pos: Number,
parent: String,
icon: String,
data: {
heading: String,
layout: String,
structure: [dataStructureSchema]
}
});
let ElementModel = mongoose.model("ElementCollection", elementSchema);
And these two Requests:
POST
app.route('/element/:id').post(function(req, res) {
let newElement = new ElementModel(req.body);
newElement.id = req.params.id;
console.log('Request Body:' + req.body);
newElement.save((err)=>{
if(err) {
res.json({info: 'error at creating Element, error:', err});
}
res.json({info: 'Element saved successfully', data: newElement});
});
});
GET
app.get('/datastructure/:id', function (req, res) {
let query = {id: req.params.id};
DataStructureModel.findOne(query, function(err, ds) {
if (err) {
res.json({info: 'error at get request', error: err});
};
if (ds) {
res.json({info: 'Datastructure found successfully', data: ds});
} else {
res.json({info: 'No such Datastructure found with id:' +req.params.id});
}
});
});
Now i want to use my Post Request to insert an Element Document aswell as the related Datastructure Document.
But it seems like my Post Request is not saving anything into the Datastructure Collection because the GET request only responses with an empty array.
I'm thankful for any help!
via Kief_15
No comments:
Post a Comment