user11543110
user11543110

Reputation:

How to get data from database in array format using node js and MySql

I am using node.js as server language and Mysql as database so I am running query and getting data from database but is is showing in format like this

  [ BinaryRow { name: 'Dheeraj', amount: '77.0000' },
    BinaryRow { name: 'Raju', amount: '255.0000' } ]

What I want is

    ['Dheeraj', 77.0000],
    ['Raju', 66255.000030],

This what I am doing in my backend (node.js):

My model:

static getChartData(phoneNo, userType) {

        let sql = 'select businessname as name,sum(billamt) amount from cashbackdispdets where consphoneno =' + phoneNo + ' group by  businessid order by tstime desc limit 10'
        return db.execute(sql, [phoneNo]);

My controller:

exports.getColumnChart = function(req, res) {
    const phoneNo = req.body.userId
    const userType = req.body.userType
    console.log(phoneNo)
    dashboardModule.getChartData(phoneNo, userType)
        .then(([rows]) => {
            if (rows.length > 0) {
                console.log(rows)
                return res.json(rows)
            } else {
                console.log("error")
                return res.status(404).json({ error: 'Phone No. already taken' })
            }
        })
    .catch((error) => {
        console.log(error)
        return res.status(404).json({ error: 'Something went wrong !!' })
    })
}

I am sending this data to Ui and when I am receiving it on UI it is in the form of object inside array which is not the required data type I want

axios().post('/api/v1/Dashboard/DashboardColumnChart',this.form)
  .then(res=>{
    console.log(res.data)
    debugger
  this.chartData= res.data
       })

The above code consoles on browser like this

I am not getting any idea how o do it should I do it with backend or with front end and how

Upvotes: 0

Views: 6029

Answers (2)

Hidayat Arghandabi
Hidayat Arghandabi

Reputation: 194

Nodejs will send you a JSON response if you want to change it. It is better to change or maniuplate it in a Front end framework. But if you want to change it in backend as you have asked Make sure that the rows is in the format that you want to recive.

 let data = [ 
        { "name": "Dheeraj", "amount": "77.0000" }, 
        { "name": "Raju", "amount": "255.0000" } 
    ]
    // empty array to store the data
    let testData = [];
    data.forEach(element => {
          testData.push(element.name)
    });

Upvotes: 2

Hussain Nawaz Lalee
Hussain Nawaz Lalee

Reputation: 896

You can format it using array.map and Object.values. map functions loops over each element and returns a modified element according to the callback provided. Object.values simply returns all the values of an object in an array.

const data = [ { "name": "Dheeraj", "amount": "77.0000" }, { "name": "Raju", "amount": "255.0000" } ];

const formattedData = data.map(obj => Object.values(obj));

console.log("Initial Data: ", data);
console.log("Formatted Data: ", formattedData);


// Map function example
const a = [1,2,3]
const mappedA = a.map(e => e * 2)
console.log(a, " mapped to: ", mappedA);

// Object.values example
const b = { firstName: 'John', lastName: 'Doe', number: '120120' }
console.log(Object.values(b));

Upvotes: 0

Related Questions