0

I have a mysql query executed from NodeJS using mysql npm package.

Upon success, I want to read the value in Javascript.

Both are on the same domain.

On console.log the value I read is 3.

How can I retrieve the data object values?

var mysql      = require('mysql');
var connection = mysql.createConnection(connectionOptions);

connection.query(
  "SELECT * FROM pruebas.Usuarios2 where RFC = '123'", 
  (err, result, fields) => { 
     if (result.length) { 
       // here I need an instruction that can send me the result of 3
       // to my javascript console
     } else { 
       console.log('no login');
     }
   }
 );
2
  • Have you read npmjs.com/package/mysql#performing-queries ? You can try to add console.log(JSON.stringify({ results, fields })); within if (result.length) { } block Commented Dec 21, 2018 at 19:58
  • How are you calling this? From where? Is this running on some sort of server? Think we need some more explanation. Commented Dec 21, 2018 at 20:45

1 Answer 1

1

You don't send from Node to Javascript. You call Node from Javascript using an Ajax call and return a value.

The answer to this question is a good exemple of how to do it: Basic Ajax send/receive with node.js

Sign up to request clarification or add additional context in comments.

6 Comments

I guess this depends on how OP has set things up.
The only way I can think this can work in another way is setting a socket connection. Can you give an example of what you're thinking ?
As part of a response from say Express?
It's still an ajax call from js to Node isn't it ?
Can just be on a page load. If you make a call to certain endpoint, let the page refresh, and let express do its work (including the query) and return it. Not AJAX required.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.