nodejs & MySQL returning insertId issue

104 views
Skip to first unread message

Mat Klik

unread,
Jan 24, 2018, 9:44:39 PM1/24/18
to nodejs
Hi All,

As someone who is currently learning nodejs I've ran into a small issue and I'm now hitting my head against a wall.

I'm successfully on a POST request checking if a user exists and then inserting a new record against their account based on their ID.

Where I'm failing down is I can't retrieve the last inserted ID after new record creation. Documentation says to use: .insertId this returns 0 on every instance.

Could someone please give me a few pointers in the right direction?

Much appreciated.

Mat

Mark Volkmann

unread,
Jan 25, 2018, 6:27:02 PM1/25/18
to nod...@googlegroups.com
Look at the insert function in this library: https://github.com/mvolkmann/mysql-easier/blob/master/src/sql-util.js
The trick is to “select last_insert_id()” after the insert.
You could also just use mysql-easier from your Node app. That’s what I do.
https://www.npmjs.com/package/mysql-easier

---
R. Mark Volkmann
Object Computing, Inc.
--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/2b30903b-7061-49d5-8087-9510c723a55c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages