0

so I have an array from another function that passes res which is a list looking like this:

[ RowDataPacket { UserID: 26 }, RowDataPacker { UserID: 4 } ]

it stores user id's, what I want is a function that finds the user id's username, and stores them in another array. This is what I have:

function getThem(res, params) {
        var promises = res.map(function (item) { // return array of promises
            // return the promise:
                    for (i = 0; i < Object.keys(res).length; i++) {
                        console.log("user: ", res[i].UserId);
                        getUsernameFromId(res[users.length].UserId).then(function() {
                            console.log("username: ", res[0].username);
                            users.push(res[0].username); 
                        });
                    }
                }, function (err) {
                    console.error(err);
                });
        Promise.all(promises).then(function () {
            console.log("users: ", users);
            //do something with the finalized list of albums here
        });
    }

output in console:

user: 26
user: 4
user: 26
user: 4
users: []
username: undefined
username: undefined
username: undefined
username: undefined

so how can I wait for the for loop to complete the mysql call? Maybe there is another way of doing this?

edit: don't mind the undefined usernames, it's easy to fix later. Just tell me how I can have those undefined inside an array

4
  • 1
    see where you have a commen // return the promise: ... well, you aren't Commented Nov 5, 2017 at 3:37
  • where can I return it to? The example I got this from they were returning it into another function and .then(function() { // for loop here}) Commented Nov 5, 2017 at 3:41
  • Your .map() callback does not return a promise. Commented Nov 5, 2017 at 3:55
  • you're performing res.map and within that loop, you're performing a loop on Object.keys(res) - that looks all kinds of wrong - and anyway, [ RowDataPacket { UserID: 26 }, RowDataPacker { UserID: 4 } ] isn't valid javascript anything Commented Nov 5, 2017 at 3:59

1 Answer 1

2

Assuming (have to assume, because your code seems to use res like a majick object that has everything you need before you do anything with it) the actual res looks like

[ { UserID: 26 }, { UserID: 4 } ]

and getUsernameFromId returns an object with a username property, like

{ username: 'blah', ...otherproperties }

getThem can be simply

function getThem(res, params) {
    return Promise.all(res.map(({UserID}) => getUsernameFromId(UserId).then(({username}) => username)))
    .then(users => {
        console.log("users: ", users);
        //do something with the finalized list of albums here
    });
}

or in "old school" javascript

function getThem(res, params) {
    return Promise.all(res.map(function (_ref) {
        var UserID = _ref.UserID;
        return getUsernameFromId(UserId).then(function (_ref2) {
            var username = _ref2.username;
            return username;
        });
    })).then(function (users) {
        console.log("users: ", users);
        //do something with the finalized list of albums here
    });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.