0

sampleGroupOfStores is an array of 100 objects

The console logs inside the map show things correct

but console.log(storeResults) is just an array of 100 undefined items

I think I'm not returning things correctly here inside map to make the new array of objects storeResults.

Any tips would be greatly appreciated.

i removed any sensitive data from the script below

I was using .forEach but I decided .map is what I wanted because I want to take sampleGroupOfStores and get a new array of objects out of it based on the criteria in the if statements below.

The setTimeOut was because I was thinking the console.log(storeReults) was running before the map fully finished.

const storeResults = sampleGroupOfStores.map( config => {
    exec(config, command, (error, response) => {
        if(error) {
            console.log('ERROR: Store: ', config.store, ' Message: ', error.level)
            return config.store + error.level
        }
        const mac1 = _.includes(response, 'MAC ADDRESS')
        const mac2 = _.includes(response, 'MAC ADDRESS')
        const mac3 = _.includes(response, 'MAC ADDRESS')
        if(response && mac1 || mac2 || mac3) {
            console.log(config.store, 'MAC PRESENT')
            return config.store + 'MAC PRESENT'
        } else {
            console.log(config.store, 'No MAC PRESENT')
            return config.store + 'No MAC PRESENT'
        }
    })
} )

setTimeout( () => {console.log(storeResults)}, 10000)

EDIT:

After some comments I change it to below and I now have some results it's just ugly. I'm working out how to clean it up a bit now so I end up with a new array of objects that include, store number and exec results.

Also this is the NPM for exec https://www.npmjs.com/package/node-ssh-exec

const storeResults = sampleGroupOfStores.map( config => {

    const storeInfo = []

    exec(config, command, (error, response) => {
        if(error) {
            //console.log('ERROR: Store: ', config.store, ' Message: ', error.level)
            storeInfo.push({store: config.store, message: error.level })
        }
        const mac1 = _.includes(response, 'MAC ADDRESS')
        const mac2 = _.includes(response, 'MAC ADDRESS')
        const mac3 = _.includes(response, 'MAC ADDRESS')
        if(response && mac1 || mac2 || mac3) {
            //console.log(config.store, 'MAC PRESENT')
            storeInfo.push({store: config.store, message: 'MAC PRESENT'})
        } else {
            //console.log(config.store, 'No MAC PRESENT')
            storeInfo.push({store: config.store, message: 'No MAC PRESENT'})
        }
    })
    return storeInfo
} )

setTimeout( () => {console.log(storeResults)}, 10000)
6
  • 4
    You are returning object from exec function's callback, not map function's callback. Commented Oct 31, 2017 at 17:38
  • 2
    The function you pass to sampleGroupOfStores.map doesn't return anything.... anyways, you cannot synchronously map an array and perform an asynchronous call in the map function. You should look into promises and Promise.all. Commented Oct 31, 2017 at 17:39
  • @theGleep: What do you think exec returns? Commented Oct 31, 2017 at 17:39
  • Ok, so I need to build like results = [] outside of exec, then inside exec inside the if statements I can push config.store + message or something like that? Commented Oct 31, 2017 at 17:41
  • @SirFry only if exec is synchronous. If it's asynchronous, what you're trying to do isn't possible, you'll have to return a promise or allow for a callback. Commented Oct 31, 2017 at 17:42

1 Answer 1

1

Looking at the code for exec(), you aren't going to get any value out of it (the result of exec() will always be undefined

And since that result is what gets passed back out to map to be added into the final outcome ... you're getting an array of undefined.

It looks like you'll need to re-think your strategy so that you're updating your array from within the callback.

I tend to do as you are considering with results = [] - and then inside of callback, I use results.push(usefulValue)

When you do that, you'll want to stop using .map()

.map() converts one array to another array by calling a method and building the output array from the responses.

So to change to using an "external" array, you'll want to use a for loop instead of .map()

Sign up to request clarification or add additional context in comments.

7 Comments

This is what I ended up doing under EDIT: in the original post I believe. My only problem now is i'm trying to figure out how to build some kind of return that looks good. Right now I'm getting an array with arrays inside it that have an object in it. I think that's because I'm using .push to push an objects. I'm trying to take what is there and format a result to look like. [ { store: NUM, message: 'message' }, { store: NUM, message: 'message' }, { store: NUM, message: 'message' } ]
You're still using .map - you'll probably want to use .forEach instead. What you're ending up with is an sampleGroupOfStores.length copies of storeInfo
I'm not sure what you mean :( sorry
... I'll expand my answer.
given your current code, it should work if you use storeInfo instead of storeResults. storeResults is the array built by .map, where storeInfo is the array you built using .push()
|

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.