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

  • SEARCH
  • Home
  • 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 7707499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:22:32+00:00 2026-06-01T00:22:32+00:00

I’ve been working on the database for a very basic social networking site: all

  • 0

I’ve been working on the database for a very basic social networking site: all you can do is register for an account and send/accept friend requests. The server is written in Javascript (NodeJS)

I have a method getUser(username) that accesses the database to get the JSON object representing a particular user given their username, and a method to list the friends of a particular user. From the listFriends(username) function, I want to return an array of the user objects rather than just their usernames and ideally I would like to utilize my own get() function rather than altering the SQL query that listFriends() uses

It might be easier to understand with an example. If I have three tables:

TABLE: UsersName
    username (unique) | firstName | lastName   |
    ------------------|-----------|------------|
    pjfry             | Phillip   | Fry        |
    proff             | Professor | Farnsworth |
    bender            | Bender    | Rodriguez  |

TABLE: UsersProfile (their profile description)
    username (unique) | description            |
    ------------------|------------------------|
    pjfry             | I went to the future   |
    proff             | I am very old          |
    bender            | Destroy all humans     |

TABLE: Friendships (for simplicity assume that if (a,b) is an entry then so is (b,a))
    user1      | user2
    -----------|---------------
    bender     | pjfry
    pjfry      | bender
    pjfry      | proff
    proff      | pjfry

And a function to get the user object:

//the callback accepts the user object
function get (username, callback) {
    db.query(
        'select * from (UsersName n, UsersProfile p) where n.username=p.username and n.username=\'' + username + '\'',
        function (rows) {
            //for simplicity assume that this call always succeeds
            //the user object is altered a bit:
            callback({
                username: rows[0].username,
                name: {
                    first: rows[0].firstName,
                    last: rows[0].lastName,
                    full: rows[0].firstName + ' ' + rows[0].lastName
                },
                profile: {
                    description: rows[0].description
                }
            });
        }
}

And here is the function to list the friends of a given user

//callback accepts the array of friends
function listFriends(username, callback) {
    db.query(
        'select user2 from Friendships where user1=\'' + username + '\'',
        function(rows) {
            //assume this call always succeeds
            callback(rows)
        }
    )
}

The problem here is that listFriends() will just return the array of usernames rather than user objects. How could I modify the listFriends() function so it returns the user objects by utilizing the get() function?

It could be done by modifying the SQL statement in listFriends() but it would be much cleaner to use the get() method so that if the structure of the user object is ever changed, it only needs to be changed in one place.

  • 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-06-01T00:22:34+00:00Added an answer on June 1, 2026 at 12:22 am

    Trying to keep things as DRY as possible, something like this should fit your requirements:

    // Disclaimer: I have not run tested this code myself
    
    //the callback accepts the user object
    function get(username, callback) {
      getFriendsWithConditions('n.username=\'' + username + '\'', function(rows){
        // returns only a single object to the input callback
        callback(rows[0]);
      });
    }
    
    //callback accepts the array of friends
    function listFriends(username, callback) {    
      getFriendsWithConditions('n.username in (select user2 from Friendships where user1=\'' + username + '\')', callback);
    }
    
    function getFriendsWithConditions(whereCondition, callback) {
      db.query(
        'select * from (UsersName n, UsersProfile p) where n.username=p.username and (' + whereCondition + ')',
        function(rows) {
          // Using ECMAScript 5 Array.map
          callback(rows.map(rowToUserObject));
        }
      );
    }
    
    function rowToUserObject(row)
    {
      return {
        username: row.username,
        name: {
          first: row.firstName,
          last: row.lastName,
          full: row.firstName + ' ' + row.lastName
        },
        profile: {
          description: row.description
        }
      };
    }
    

    A few things that came to mind as I was writing this:

    1. UserName and UserProfile feel like they should only be one table, but for all I know this is a simplified version of your actual database, so I left them as is.

    2. I used Array.map as defined in ECMAScript 5 which should be available in Node.

    3. The SQL alias names for each table are hardcoded throughout each function, meaning that you would also need to a) set some sort of constant or come of with a convention to keep all aliases consistent throughout all your functions (which can be a pain to maintain as the project grows) or b) look into using an ORM that can do these things for you (and more!) This will help you avoid sweating the details of how to construct queries, leaving you to worry about more important things, like what data you actually need. I’m not terribly familiar with what’s available for NodeJS in terms of ORMs, but the official NodeJS wiki should be a good place start.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I have a reasonable size flat file database of text documents mostly saved in
I'm parsing an XML file, the creators of it stuck in a bunch social
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

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.