#Paths

52 messages · Page 1 of 1 (latest)

gray ravine
#
            const files = await fs.readdir("tiers")
            console.log(files)
``` Why is it not printing anything?
polar orbit
#

prolly the code execution doesn't even reach those lines

gray ravine
#

nvm

#

i am dumb

#

wait no @polar orbit can u explain i tough i figured it out but i didn't

polar orbit
#

because you returned out of the function

gray ravine
polar orbit
#

show the actual full code

gray ravine
#

i want to acces that itemId variable from rl.on but i can't

polar orbit
#

because it's in a different scope 😳

#

either save the result to a variable or perform it within the callback function

gray ravine
#

but how can i save it to a varible since i can't acces if out of rl.on

polar orbit
#

literally:

let itemId;
// in callback fn
itemId = line;
// out of callback fn
url = itemId;
gray ravine
#

woooh

#

wotks

#

works

polar orbit
#

👍

gray ravine
polar orbit
#

must be because the callback is an async function

#

so it logs undefined with the async or sync callback fn?

gray ravine
#

all this code is in a async function

polar orbit
# gray ravine

what if it's because that event isn't even triggered 🤔

gray ravine
#

now there is 1 more thing is there any shortcut in node.js to compare 2 arrays?

#

and see in one of the element matches?

#

or i need to do it with a for loop

frank sphinx
#

What is the end goal here? Just reading the content of the file?

gray ravine
#

no i am doing a bot that checks a file full of codes for a given keyword by the user let's say the user enters the input dagger my program needs to find that keyword in list of codes and eventually he will find the code that will look like this WEAPON_DAGGER

frank sphinx
#

Because you used the callback variant of readdir while readdirSync and promise variant of readdir exist too. Same for readFile

gray ravine
#

but the thing is not every code has the word in position 1 something that can match with the user input

#

so i can't just do .split('_')[1]

frank sphinx
#

Is it json or yaml or random unformatted file?

gray ravine
#

unformated file

#

looks like this

frank sphinx
#

With one word on every line? Not that unformatted then. Just read the whole file as array of lines and use .find(…)

sour starBOT
#

mdn Array.prototype.find()
The find() method returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.

gray ravine
#

but find won't return me a list if i have like WEAPON_DAGGER and WEAPON_DAGGER_2?

frank sphinx
#

Then use filter instead

gray ravine
#

uhh idk to use that but i can look it up

#

thanks

sour starBOT
#

mdn Array.prototype.filter()
The filter() method creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function.

gray ravine
#

@frank sphinx isn't there smth like .findfirst?

polar orbit
#

unless you mean find index?

gray ravine
#

no i mean find element matching a string

polar orbit
#

use that first method Qjuh mentioned

gray ravine
#

because now i get the response {my input} is not a function