retrieving an item through a consultation using JavaScript

I'm having an issue where I am successfully retrieving a value from a query, everything seems to be working fine. However, when I try to assign it to a const, the object does not get passed.

  let selectid = await pool.query(
    `SELECT ID_user FROM user WHERE User_email='${email_consulted}'`
  );

  const id = selectid.ID_user

Any ideas on what could be causing this problem?

Answer №1

As per the information provided in this demonstration (available at: ), when using pool.query, it returns a promise where the result is an object containing a 'rows' property:

const { Pool } = require('pg')
const pool = new Pool()
pool.query('SELECT $1::text as name', ['brianc'], (err, result) => {
  if (err) {
    return console.error('Error executing query', err.stack)
  }
  console.log(result.rows[0].name) // brianc
})

If you are utilizing 'await', you can access the result entity like so:

const id = selectid.rows[0].ID_user

Answer №2

After running the query, you will receive an array that needs to be indexed correctly. This can be achieved with the following code:

const userId = selectedIds[0].user_ID

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Combining Json attributes in Jquery Grouping

My task is to group the displays by sectors, but I couldn't find a method in JSON to achieve this. Below is the code snippet: $(function displays(){ var url = '{% url get_displays %}'; $.getJSON(url, function(data) { var sidebar = ...

conceal the bootstrap navigation bar while the page is being

Struggling to toggle a Bootstrap navbar on scroll? Need some guidance from the pros out there. I admit, my Bootstrap and jQuery skills are pretty basic. The browser console doesn't throw any errors, and I've experimented with fadeIn, fadeOut, add ...

What are some methods for substituting standard JavaScript built-in objects?

My client has developed their own app using Chromium, with a navigator.appVersion of AppleWebkit/534+ https://i.sstatic.net/zEZy6.jpg To my surprise, they have replaced the standard JavaScript built-in objects with their own version. For instance, take a ...

Is it necessary for me to use a .jsx extension when saving my React component files?

After working with React for a few months, I recently noticed that some of my files have the .js extension while others have the .jsx extension. Surprisingly, when I write JSX code in the .js files, everything still functions correctly. Is there any signif ...

Obtain an ASP.NET Label control with the use of JavaScript

I am experiencing difficulty obtaining the id of a label control named tb_TA_2_6 within a form view using JavaScript. I have attempted the following: <script type ="text/jscript" language= "javascript" > function autosum(t1, t2) { var sum; var a = ...

Having trouble retrieving data with the componentDidMount API call in React?

I'm currently in the process of building a Gallery feature. Initially, I hardcoded the image links, but now I want to fetch them using an API call. The implementation didn't go as planned, and it's still pulling the hardcoded images from the ...

Guide on sending JSON messages between Node.js services within a Docker Compose environment

If I have two Node.js services running in a Docker Compose, one listening on port 4000 and the other on port 5000, how can I exchange JSON messages between them using Express? ...

Guide to adding a new Vue-Grid-Item with a button

Currently, I am working on a software project for the Research department at my university, specifically focusing on an Atomic Layer Deposition system. The main goal of this program is to allow users to create and customize their own 'recipe' for ...

Converting MySQL data to JSON format in PHP, including handling nested objects

Hey there, I'm currently working on organizing these results into arrays in PHP so that I can convert them into JSON objects and send them over to the client. Here is what the query results look like: id name hours cat status 3bf JFK Int 24 ...

How to Invoke JavaScript Functions within jQuery Functions?

I'm working on a jQuery function that involves dynamically loading images and checking their width. I have two JavaScript functions, func1() and func2(), that I want to call from within the jQuery function in order to check the width of each image. T ...

Is there a way to display an XML listing recursively similar to the functionality of an ASP:MENU in the past?

I have been working on converting a previous asp:menu item to JavaScript. Here is the JavaScript code I have come up with: function GetMainMenu() { var html = ''; var finalHTML = ''; finalHTML += '<d ...

Completely charting the dimensions of an unfamiliar object (and the most effective method for determining its extent)

I am facing a challenge with a "parent" object that contains an unknown number of children at various depths. My main objective is to efficiently map this "parent" object and all its levels of children. How can I achieve this task? Each child already inc ...

Error alert: Unable to find the specified word

Having trouble writing a word to the database due to an error: ReferenceError: Patikrinta is not defined. Below is my ajax script for sending data to a PHP file, and then the PHP script itself if needed. Haven't found a solution on Stack Overflow. $s ...

I encounter difficulties using my static resources in the root route of my Express.js application

Can you guide me on how to implement styles and images from the assets folder in my webpage, specifically for the root route? As an example, I have a styles.css file located at assets/styles.css. In my code, I am using app.use(express.static('assets&a ...

Interact with the horizontal click and drag scroller to navigate through various sections effortlessly, each designed to assist you

Currently, I am facing an issue with my horizontal scrolling feature in JavaScript. It works perfectly for one specific section that has a particular classname, but it fails to replicate the same effects for other sections that share the same classname. ...

Tips for modifying AJAX behavior or restricting requests in Wicket

I am looking to prevent updates based on a specific JavaScript condition using AjaxSelfUpdatingTimerBehavior. WebMarkupContainer messagesWmc = new WebMarkupContainer( "messagesWmc" ) ; messagesWmc.setOutputMarkupId( true ) ; messagesWmc.add( ...

What steps should I take to adjust this PHP script so that it can display nested outcomes?

My aim is to transform this array into a single object to facilitate working with a JObject on the frontend. Currently, I have an array but I am uncertain about how to modify it to return a single object instead. This snippet shows the code: $contacts = ...

What is the reason in AngularJS for requiring directive attributes to be hyphen-separated while their scope variables are camelCased?

Here is an example in Html: <!-- Note 'display-when' is hyphenated --> <wait-cursor display-when="true"></wait-cursor> Then, when defining it in the directive: scope: { // Note 'displayWhen' is camelCased show: ...

Storing Multidimensional Arrays in a Database Using Sequelize

My JSON file contains nested arrays with varying lengths, meaning each object has an array with a different number of objects. { "count": 200, "objects": [ { "id": "FIRST", "b": "two", ...

Dynamic collapsible containers

I discovered a useful feature on w3schools for collapsing elements: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_collapsible_symbol However, I would like to reverse it so that all elements are initially shown when the page loads, and then ...