[Vuejs]-Axios and sequelize communication

0👍

Finally resolved my answer using this post :How to correctly use axios params with arrays

This is how my Vue.js axios GET query looks now :

import qs from 'qs';

 axios.get("/api/employees", {
            "params": {
                attributes: ['lastName', 'firstName'],
                where:{id:2}
            },
            paramsSerializer: params => {
                return qs.stringify(params)
            }
        }).then((response) => {
            this.employees = response.data;
        }).catch((error) => {
            console.log(error.response.data);
        });

And how my SEQUELIZE node back end looks :

// =========
    // Read many
    // =========
    const readMany = (req, res) => {
  
        model.findAll(
                req.query
            )
            .then(function(dbModel) {
                res.json(dbModel);
            })
            .catch(function(err) {
                res.json(err);
            })
    };

Have a nice day, bro !

Edit : Still have an ugly code when doing jointures :

My vue.js axios :

  axios.get("/api/employees", {
                "params": {
                    where: {
                        id: 2
                    },
                    include: [{
                        model: "offices"
                    }]
                },
                paramsSerializer: params => {
                    return qs.stringify(params)
                }
            }).then((response) => {
                this.employees = response.data;
            }).catch((error) => {
                console.log(error.response.data);
            });

My node server sequelize back end: Ugly , but it works.

// =========
    // Read many
    // =========
    const readMany = (req, res) => {

        req.query.include[0].model = eval("models." + req.query.include[0].model)

        model.findAll(
                req.query
            )
            .then(function(dbModel) {
                res.json(dbModel);
            })
            .catch(function(err) {
                res.json(err);
            })
    };

I need this because models are generated by sequelize-auto , don’t know how to do , it’s ok like that. You can peep more demos on my github : nicolas15000

Leave a comment