I experiment problems with an AXIOS GET query to my node sequelize back end .
axios.get("/api/employees", {
"params": {
"where": {
"id": 2
},
"include": {
"name": "offices"
}
}
}).then((response) => {
this.employees = response.data;
}).catch((error) => {
console.log(error.response.data);
});
My node web service doesn't accept this query in the findAll() function :
const readMany = (req, res) => {
model.findAll(req.query)
.then(function(dbModel) {
res.json(dbModel);
})
.catch(function(err) {
res.json(err);
})
};
I have been forced to write this ugly code, to make it work :
const readMany = (req, res) => {
// RECEIVING WHERE PARAMS
if (req.query.where) {
req.query.where = JSON.parse(req.query.where)
}
// RECEIVING INCLUDE PARAMS
if (req.query.include) {
var myobj = JSON.parse(req.query.include);
var my_model = eval("models." + myobj.name)
}
// RECEIVING ORDER PARAMS
if (req.query.order) {
req.query.order = JSON.parse(req.query.order)
}
model.findAll({
where: req.query.where,
include: my_model,
order:req.query.order
})
.then(function(dbModel) {
res.json(dbModel);
})
.catch(function(err) {
res.json(err);
})
};
Maybe you have a workaround ? Why do I have to convert to a JSON, and to separate like this ? findAll() should accept an entire JSON object directly coming from Axios, no ?
Please have Look what a console.log(req.body) tells :
{ where: '{"id":2}' }
Please notice how much the quotes are weird, I can't even remove them !