XDevApi How to Send Back the Result of .insert()

91 Views Asked by At

.insert() is successful, but the following resulting {result:{}} in Postman. How do I send the whole object in screen back so the client can query properties?

enter image description here

1

There are 1 best solutions below

0
On BEST ANSWER

You can't because the Result instance isn't directly serializable to JSON, which in the end, is the format that gets sent back to the client via the HTTP response. You need to build a plain JavaScript object on the server side containing whatever properties you need, which in turn will be properly serialized to JSON.

In the case of a regular insert(), eventually, the only "interesting" data might be the number of items that were inserted, the AUTO_INCREMENT count (when it applies) and details about any warnings that have been generated. Everything else is not applicable to this kind of operation.

table.insert(/* something */)
  .execute()
  .then(out => {
    const json = {
      items: out.getAffectedItemsCount(),
      auto_increment: out.getAutoIncrementValue()
    }

    if (out.getWarningsCount() > 0) {
      json.warnings = out.getWarnings()
    }

    res.status(200).send(json)
  }) 

Disclaimer: I'm the lead developer of the MySQL X DevAPI Connector for Node.js