简体   繁体   中英

Add a new row to result of sql query

I want to add a row to result of mysql query in nodejs

But I can't figure out how to do it, It seems the result is array of RowDataPacket ,but I didn't find a way to push a row or make RowDataPacket

My simplified version of code is :

var sql="select * from category";
db.query(sql,  function (err, result) {
    if (err)
        throw err;
    if (result[0] != undefined) {
        var data = result;
        if(user_id=='...'){
            data.push({'cat_id': "1", 'cat_order': "5",'created_category':"0",'created_user_category':'2'
                ,'status':'1','name':'test','img':'test.jpg'
                ,'max_created':new Date() / 1000});
            console.log(data);//error:Converting circular structure to JSON
            response.json({'result': "yes", 'data': data,'error':''});//error
            return;
        }
        response.json({'result': "yes", 'data': data,'error':''});
    } else {
        response.json({'result': "no",'error':'wrong input', 'data': []});
    }
});

How can I add a row to result of sql query?

It seems ,I used data in the object that I was pushing .so obviously it was circular structure ( data.push(...'cat_order': data,...))

And I remove it in the simplified code that I use in the question .

So the code In the question works just fine :)

var sql="select * from category";
db.query(sql,  function (err, result) {
    if (err)
        throw err;
    if (result[0] != undefined) {
        var data = result;
        if(user_id=='...'){
            data.push({'cat_id': "1", 'cat_order': "5",'created_category':"0",'created_user_category':'2'
                ,'status':'1','name':'test','img':'test.jpg'
                ,'max_created':new Date() / 1000});
            console.log(data);
            response.json({'result': "yes", 'data': data,'error':''});
            return;
        }
        response.json({'result': "yes", 'data': data,'error':''});
    } else {
        response.json({'result': "no",'error':'wrong input', 'data': []});
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM