Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6050333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:40:36+00:00 2026-05-23T07:40:36+00:00

In my node.js app, I have the following code to get the last mark

  • 0

In my node.js app, I have the following code to get the last mark of a given user.

 app.get('/user/:token/mark/last', function(req, res){
    var user_token   = req.params.id;

    // Mark to be returned
    var last_mark = 0;

    // Get user
    var get_user = client.query("SELECT id FROM user where user_token = $1", [user_token]);

    // Get mark: handle sensor retrieved
    get_user.on('row', function(row) {
      get_mark = client.query("SELECT * FROM mark WHERE user_id = $1 order by date desc limit 1", [row.id]);


      get_mark.on('row', function(mark) {
        last_mark = mark.value;
        console.log("LAST MARK:" + last_mark);    // OK IT DISPLAYS "LAST MARK:16"
      });

      // Error checking
      get_mark.on("error", function (err) {
        console.log("ERROR");
      });
    });

    // Check database errors
    get_user.on("error", function (err) {
      console.log("NO SUCH USER");
    });

    // Finalize
    get_user.on("end", function () {
      console.log("LAST MARK:" + last_mark);   // KO IT DISPLAYS "LAST MARK: 0"
    });
 })

The problem is I do not get the correct “last_mark” (I get 0), as if the assignement was not done.

Do you have any idea ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:40:36+00:00Added an answer on May 23, 2026 at 7:40 am

    This isn’t my best answer ever but you could replace your two queries with one that does a JOIN:

    select m.value
    from mark m join user u on (m.user_id = u.id)
    where user_token = $1
    order by m.date desc
    limit 1
    

    That should make the problem go away and let you get on with your work. As Linus said:

    No problem is too big it can’t be run away from.

    I still think there is some sort of timing issue with when the inner “row is ready” event is fired and the outer “query is finished” event is fired. I would expect the outer “end” event to be fired before the inner “row is ready” event (but, admittedly, I’m not familiar with the node.js database interface). You could also push the “deal with last_mark” logic into the “row is ready” callback:

    get_mark.on('row', function(mark) {
        // Call yet another callback to send `mark.value`
        // into the outside world.
    });
    

    That would introduce more steps in the callback chain but this sort of thing is par for the course when dealing with asynchronous APIs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following HTML node structure: <div id=foo> <div id=bar></div> <div id=baz> <div
I have an Android App with 2 activities. I have the following in the
I have a project with a manifest file with the following node: <requestedExecutionLevel level=requireAdministrator
I have been trying to figure out how to integrate a node.js app into
I have running nginx on my server ansel.ms and a node.js app on ansel.ms:46156.
I have the following method in an App_Code/Globals.cs file: public static XmlDataSource getXmlSourceFromOrgid(int orgid)
I have the following (example) of my xml document: <Students> <Student ID = *GUID
I have a .Net app running in IIS6. The app dlls are compiled against
We have a .net 3.5 web app that consumes third party web services. The
.NET 3.5, C# I have a web app with a search feature. Some of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.