How to access a RowDataPacket object

前端 未结 14 2340
悲&欢浪女
悲&欢浪女 2020-11-29 03:02

I\'m currently developing a desktop application with Node-webkit. During that process I need to get some data from a local MySQL-database.

The querying works fine, b

相关标签:
14条回答
  • I found an easy way

    Object.prototype.parseSqlResult = function () {
        return JSON.parse(JSON.stringify(this[0]))
    }
    

    At db layer do the parsing as

    let users= await util.knex.raw('select * from user')
        return users.parseSqlResult()
    

    This will return elements as normal JSON array.

    0 讨论(0)
  • 2020-11-29 03:11

    I had this problem when trying to consume a value returned from a stored procedure.

    console.log(result[0]);
    

    would output "[ RowDataPacket { datetime: '2019-11-15 16:37:05' } ]".

    I found that

    console.log(results[0][0].datetime);
    

    Gave me the value I wanted.

    0 讨论(0)
  • 2020-11-29 03:12

    Turns out they are normal objects and you can access them through user_id.

    RowDataPacket is actually the name of the constructor function that creates an object, it would look like this new RowDataPacket(user_id, ...). You can check by accessing its name [0].constructor.name

    If the result is an array, you would have to use [0].user_id.

    0 讨论(0)
  • 2020-11-29 03:12

    Solution

    Just do: JSON.stringify(results)

    0 讨论(0)
  • 2020-11-29 03:13

    I also met the same problem recently, when I use waterline in express project for complex queries ,use the SQL statement to query.

    this is my solution: first transform the return value(RowDataPacket object) into string, and then convert this string into the json object.

    The following is code :

    //select all user (查询全部用户)
    find: function(req, res, next){
        console.log("i am in user find list");
        var sql="select * from tb_user";
    
        req.models.tb_user.query(sql,function(err, results) {
            console.log('>> results: ', results );
            var string=JSON.stringify(results);
            console.log('>> string: ', string );
            var json =  JSON.parse(string);
            console.log('>> json: ', json);
            console.log('>> user.name: ', json[0].name);
            req.list = json;
            next();
        });
    }
    

    The following is console:

        >> results:  [ RowDataPacket {
        user_id: '2fc48bd0-a62c-11e5-9a32-a31e4e4cd6a5',
        name: 'wuwanyu',
        psw: '123',
        school: 'Northeastern university',                                                                                                                                           
        major: 'Communication engineering',                                                                                                                                            
        points: '10',
        datems: '1450514441486',
        createdAt: Sat Dec 19 2015 16:42:31 GMT+0800 (中国标准时间),                                                                                                  
        updatedAt: Sat Dec 19 2015 16:42:31 GMT+0800 (中国标准时间),                                                                                                  
        ID: 3,
        phone: 2147483647 } ]
    >> string:  [{"user_id":"2fc48bd0-a62c-11e5-9a32-a31e4e4cd6a5","name":"wuwanyu","psw":"123","school":"Northeastern university","major":"Communication engineering","points":"10","datems":"1450514
    441486","createdAt":"2015-12-19T08:42:31.000Z","updatedAt":"2015-12-19T08:42:31.000Z","ID":3,"phone":2147483647}]
    >> json:  [ { user_id: '2fc48bd0-a62c-11e5-9a32-a31e4e4cd6a5',
        name: 'wuwanyu',
        psw: '123',
        school: 'Northeastern university',                                                                                                                                           
        major: 'Communication engineering',                                                                                                                                            
        points: '10',
        datems: '1450514441486',
        createdAt: '2015-12-19T08:42:31.000Z',
        updatedAt: '2015-12-19T08:42:31.000Z',
        ID: 3,
        phone: 2147483647 } ]
    >> user.name:  wuwanyu
    
    0 讨论(0)
  • 2020-11-29 03:20

    You can copy all enumerable own properties of an object to a new one by Object.assign(target, ...sources):

    trivial_object = Object.assign({}, non_trivial_object);
    

    so in your scenario, it should be enough to change

    ret.push(i);
    

    to

    ret.push(Object.assign({}, i));
    
    0 讨论(0)
提交回复
热议问题