How to store SQL query result in a JavaScript variable to use in global scope?

Marcio Cantante

Beginner here, I'm trying to store the result of a SQL query into a JavaScript variable so I'd be able to access it from the global scope to use in other code.

Here's the code:

const latestRunContacts = () => {
  db.all(
    `SELECT email FROM Contacts_tbl WHERE RunID = (SELECT MAX(UID) FROM Run_tbl)`,
    (err, row) => {
      console.log(row);
    }
  );
};

This logs the the values I'm trying to get, but as soon as I try to do this:

const latestRunContacts = () => {
  db.all(
    `SELECT email FROM Contacts_tbl WHERE RunID = (SELECT MAX(UID) FROM Run_tbl)`,
    (err, row) => {
      return row;
    }
  );
};

console.log(latestRunContacts());

Then it returns undefined... Since typeof row is an object, I've tried declaring an object and assigning the result of the SQL query to it and calling the function immediately like so:

let contactEmail = {
  email: (function latestRunContacts() {
    db.all(`SELECT email FROM Contacts_tbl WHERE UID = '1'`, (err, row) => {
      return row;
    });
  })(),
};

console.log(contactEmail.email);

But this also just returns undefined.

Is what I'm trying to do even possible? If so, how??

myishay

This is related to callbacks and to the asynchronous nature of javascript.

A quick fix for your problem would be this:

let latestContacts;
db.all(
  `SELECT email FROM Contacts_tbl WHERE RunID = (SELECT MAX(UID) FROM Run_tbl)`,
  (err, rows) => {
    if (err) throw err;
    latestContacts = rows;
    console.log(latestContacts);
  },
);

I recommend reading this article, summarizing callbacks, promises and async functions: https://scotch.io/courses/10-need-to-know-javascript-concepts/callbacks-promises-and-async

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to store the result of a SQL statement as a variable and use the result in an SSIS Expression?

How Do I Use Dynamic SQL (MySQL) To Store The Result of a Query Into a Local Variable?

How to store a query result in a variable

How can I store a sql query into a variable, not the result?

How to store SQL query result in a variable using PHP?

How to store SensorThings query result into a variable in JavaScript/jQuery

How to store a query result into a variable in mysql and then use it another query and echo result?

Store a sql query result in pentaho variable

How do I use an SQL Query Result as the value of a variable in Java?

How to assign result of a sql query to a variable and use it into a built in function?

How to use a variable in PHP that store in SQL query in database

How to store the result of a linq query in a KeyDictionary variable

how to store the query result into a variable in mysql

How to load data from a Firestore query into a variable at the global scope for later use

How to use global variable in JavaScript?

Can I store the result of Fetch in a global variable using JavaScript?

Javascript how to store Fetch data to a global variable

How to use zustand to store the result of a query

How to store SQL Query result in table column

How to store result of is into variable in Oracle SQL

How to store the result of dynamic SQL in a variable?

How to store an sql result in a variable to be used

Javascript - How to store result of the for loop into the variable?

How to create global variable in query and usable with javascript

JavaScript scope issue with global variable

How to store response after reading from a json file using XMLHttpRequest in javascript into a global variable and use it anywhere in the program

store query result, with multiple results as list variable to use later

Store the first result value into a variable from sql query in php

How to store SQL result in a variable and parse the result to identify a possible pattern?