Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

268
Views
Expressjs variable value is not updating

In below code snippet, I declare one variable named sumOfRevenue. I set its value as 10 in router and then try to print its value but I am getting it blank. So may I know why I got blank rather then 10?

Please check below code and attached screenshot.

const express = require('express')
var router = express.Router()
const client = require('../models/db')

/* I defined sumOfRevenue variable as below */
sumOfRevenue = ""  

router.get('/getSites', async(req, res) => {
  await client.query('select sum(revenue) from deals ', (error, results) => {
    if (!error) 
      /* Here I just store static value as 10 to this variable */
      sumOfRevenue =10
    })
    /* Here when I want to get that value but it gives blank */
    console.log("sumOfRevenue is:" + sumOfRevenue);
});

module.exports = router

enter image description here

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

  • You need to add var or let to declare your variable.
  • You need to be sure "!error" is equal to "true" (console.log(!error))
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!