如何使用节点js和MySql

我使用node.js作为服务器语言,使用Mysql作为数据库,因此我正在运行查询并从数据库中获取数据,但是以这种格式显示

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

我想要的是

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

这是我在后端(node.js)中正在做的事情:

我的模特:

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]);

我的控制器:

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 !!' })
    })
}

我正在将这些数据发送到Ui,当我在UI上接收到它时,它是数组内对象的形式,这不是我想要的必需数据类型

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

上述代码控制台在

如何使用节点js和MySql

之类的浏览器上

我不知道该如何使用后端或前端以及如何操作

deipfqu850726 回答:如何使用节点js和MySql

如果要更改它,Nodejs将向您发送JSON响应。最好在前端框架中进行更改或操作。但是,如果您想按要求在后端进行更改,请确保这些行采用您要接收的格式。

 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)
    });
,

您可以使用array.mapObject.values对其进行格式化。 map函数遍历每个元素,并根据提供的回调返回修改后的元素。 Object.values只是返回数组中对象的所有值。

const data = [ { "name": "Dheeraj","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));

本文链接:https://www.f2er.com/3168307.html

大家都在问