Node MySQL: Return a value from a column


I'm using mysql in a Node application, Discord bot. I want to select a value from a column but am having trouble in doing so. This is my code:

let prefix; connection.query(`SELECT * FROM guilds WHERE guildid = ${message.guild.id}`, function(error, commands) { if (error) throw error; if (commands.length) { // the guild is in my database commands.forEach(value => prefix = value.prefix; console.log(value.prefix) // doesn't fire } } }); message.channel.send(prefix.length) // crashes [Cannot read property length of undefined]

I now set prefix in line 1 to "3"and it works as expected ( I think ), printing out the value in my database, which looks as follows:

| guildname | guildid | ownername | ownerid | prefix | ------------------------------------------------------ | MyServer | 7373732 | Rusty | 636363 | ! |


The console log event not firing would imply empty result set from the query. Although it looks like this isn't the exact code you are using, the parameter list to forEach looks wrong.

(value => prefix = value.prefix; console.log(value.prefix) // doesn't fire }

Try letting the sql engine include the variable value via sql parameters instead of using string interpolation. I mean something like:

connection.query('SELECT * FROM guilds WHERE guildid = ?',[message.guild.id], function(error, commands) { ...

<a href="https://www.sitepoint.com/using-node-mysql-javascript-client/" rel="nofollow">https://www.sitepoint.com/using-node-mysql-javascript-client/</a>

What is the value of message.guild.id?


  • Recreate the Oracle DUAL table
  • ConfigurationBuilder not working in azure function
  • How dotnet build chooses the output name
  • multidatatrigger with multibinding in ControlTemplate.Triggers
  • Can you pass an array from javascript to asp.net mvc controller action without using a form?
  • Extract All Possible Paths from Expression-Tree and evaluate them to hold TRUE
  • XSLT foreach repeating nodes to flat
  • Cloud Code function running twice
  • URLConnection doesn't work since API 10 and higher?
  • List images(01.png) and descriptions(01.txt) from directory
  • C# program and C++ DLL compiled for 32-bit system crash on 64-bit system
  • Thread safety of a fluent like class using clone() and non final fields
  • How to Cache Real-time Data?
  • Converting a WriteableBitmap image ToArray in UWP
  • debug library loaded with ctypes using gdb
  • D3 nodes and links from JSON with nested arrays of children
  • How to extract text from Word files using C#?
  • How to check if every primary key value is being referenced as foreign key in another table
  • Sending data from AppleScript to FileMaker records
  • Is there a mandatory requirement to switch app.yaml?
  • retrieve vertices with no linked edge in arangodb
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Change div Background jquery
  • How to stop GridView from loading again when I press back button?
  • Linking SubReports Without LinkChild/LinkMaster
  • apache spark aggregate function using min value
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?