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 7714037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:01:26+00:00 2026-06-01T02:01:26+00:00

What I’d like to do is add an array of students to each manager

  • 0

What I’d like to do is add an array of students to each manager (in an array).

This is where I’m getting stuck:

      for sup in sups
        do(sup) ->
          sup.students_a = "This one works"
          getStudents sup.CLKEY, (studs) ->
            sup.students_b = "This one doesn't"
      cback sups

EDIT: After some thought, what may be happening is that it is adding the “sudents_b” data to the sups array, but the sups array is being returned (via cback function) before this work is performed. Thus, I suppose I should move that work to a function and only return sups after another callback is performed?

For context, here’s the gist of this code:

odbc = require "odbc"

module.exports.run = (managerId, cback) ->
  db2 = new odbc.Database()
  conn = "dsn=mydsn;uid=myuid;pwd=mypwd;database=mydb"
  db2.open conn, (err) ->
    throw err if err

    sortBy = (key, a, b, r) ->
      r = if r then 1 else -1
      return -1*r if a[key] > b[key]
      return +1*r if b[key] > a[key]
      return 0

    getDB2Rows = (sql, params, cb) ->
      db2.query sql, params, (err, rows, def) ->
        if err? then console.log err else cb rows

    getManagers = (mid, callback) ->
      supers = []
      queue = []

      querySupers = (id, cb) ->
        sql = "select distinct mycolumns where users.id = ? and users.issupervisor = 1"
        getDB2Rows sql, [id], (rows) ->
          for row in rows
            do(row) ->
              if supers.indexOf row is -1 then supers.push row
              if queue.indexOf row is -1 then queue.push row
          cb null

      addSupers = (id) -> # todo: add limit to protect against infinate loop
        querySupers id, (done) ->
          shiftrow = queue.shift()
          if shiftrow? and shiftrow['CLKEY']? then addSupers shiftrow['CLKEY'] else
            callback supers

      addMain = (id) ->
        sql = "select mycolumns where users.id = ? and users.issupervisor = 1"
        getDB2Rows sql, [id], (rows) ->
          supers.push row for row in rows

      addMain mid
      addSupers mid

    getStudents = (sid, callb) ->
      students = []

      sql = "select mycols from mytables where myconditions and users.supervisor = ?"
      getDB2Rows sql, [sid], (datas) ->
        students.push data for data in datas
        callb students

    console.log "Compiling Array of all Managers tied to ID #{managerId}..."
    getManagers managerId, (sups) ->
      console.log "Built array of #{sups.length} managers"
      sups.sort (a,b) ->
        sortBy('MLNAME', a, b) or # manager's manager
        sortBy('LNAME', a, b) # manager
      for sup in sups
        do(sup) ->
          sup.students_a = "This one works"
          getStudents sup.CLKEY, (studs) ->
            sup.students_b = "This one doesn't"
      cback sups
  • 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-01T02:01:28+00:00Added an answer on June 1, 2026 at 2:01 am

    You are correct that your callback cback subs is executed before even the first getStudents has executed it’s callback with the studs array. Since you want to do this for a whole array, it can grow a little hairy with just a for loop.

    I always recommend async for these things:

    getter = (sup, callback) ->
      getStudents sup.CLKEY, callback
    
    async.map sups, getter, (err, results) ->
      // results is an array of results for each sup
      callback() // <-- this is where you do your final callback.
    

    Edit: Or if you want to put students on each sup, you would have this getter:

    getter = (sup, callback) ->
      getStudents sup.CLKEY, (studs) ->
        sup.students = studs
        // async expects err as the first parameter to callbacks, as is customary in node
        callback null, sup
    

    Edit: Also, you should probably follow the node custom of passing err as the first argument to all callbacks, and do proper error checking.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the

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.