0

Developing my RestAPI using XDEVAPI talking to MySQL. This piece of code works for adding a new record,

myRecord.add = (newmyRecord, res) =>
{
    mysql.getSession(mydb)      // .getSession() is a Promise, returns a session
        .then
        (
            sess =>
            {
                sess.getSchema("myschema").getTable("mytable").insert(...).execute(row=>{console.log('inserted')}); 
                
                res.send(200);   // resulting "UnhandledPromiseRejectionWarning: TypeError: res.send is not a function"
                //return 200;    // Postman still shows "Sending" and Fiddler does not show status 200
            }
            
        );
}

So my question is how to send back a successful 200 to complete the POST?

Jeb50
  • 6,272
  • 6
  • 49
  • 87

1 Answers1

1

The execute() method also returns back a Promise and, in the case of insert(), it isn't expecting any kind of callback, so the following line will never be called:

console.log('inserted')

The only instances where execute() expects callbacks are on TableSelect and CollectionFind. And we are slowly moving away from that API flavour, since now you can also process the result sets by calling fetchOne() or fetchAll() on the Result instance to which that Promise resolves to (see DocResult and RowResult).

In any case, nothing prevents that res.send(200) call to happen and nothing implicitly changes the API of the underlying HTTP framework (which you seem to be using). So, the issue you mention does not seem to be in anyway related to the MySQL X DevAPI connector.

TypeError: res.send is not a function

You are probably overriding that res object somewhere before calling it (and before calling add()).

This isn't probably of much help, but it's the only thing I can extract right now from your post.

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

ruiquelhas
  • 1,905
  • 1
  • 17
  • 17
  • although no solution provided, really appreciate the detailed explanation! I found the solution: 1) replace `res` with `result`, that is `myRecord.add = (newmyRecord, res)` to be `myRecord.add = (newmyRecord, result)`. 2) replace `res.send(200);` with `result(null);` Not very sure why, but works well. – Jeb50 Jul 18 '20 at 17:31
  • actually, no need to replace `res`, but do `res(null)` works. – Jeb50 Jul 18 '20 at 18:09
  • A follow-up question, `.insert()` supposed returns a `execute() → {Promise.}`, so according to document it returns number of rows, and others like `getAffectedItemsCount()`. But Watch doesn't show any of them available. – Jeb50 Jul 18 '20 at 18:26
  • Not sure if I'm following. If you insert one row then `result.getAffectedItemsCount()` returns `1` (the number of rows that were inserted in the scope of the operation), it does not return the total number of rows that exist in the table. – ruiquelhas Jul 19 '20 at 09:41
  • after studying, I did override the res. Good suggestion! – Jeb50 Aug 01 '20 at 21:05