How to access a RowDataPacket object

前端 未结 14 2434
悲&欢浪女
悲&欢浪女 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条回答
  •  没有蜡笔的小新
    2020-11-29 03:03

    going off of jan's answer of shallow-copying the object, another clean implementation using map function,

    High level of what this solution does: iterate through all the rows and copy the rows as valid js objects.

    // function  will be used on every row returned by the query
    const objectifyRawPacket = row => ({...row});
    
    // iterate over all items and convert the raw packet row -> js object
    const convertedResponse = results.map(objectifyRawPacket);
    

    We leveraged the array map function: it will go over every item in the array, use the item as input to the function, and insert the output of the function into the array you're assigning.

    more specifically on the objectifyRawPacket function: each time it's called its seeing the "{ RawDataPacket }" from the source array. These objects act a lot like normal objects - the "..." (spread) operator copies items from the array after the periods - essentially copying the items into the object it's being called in.

    The parens around the spread operator on the function are necessary to implicitly return an object from an arrow function.

提交回复
热议问题