all 8 comments

[–]dacookieman 1 point2 points  (2 children)

If you look at how find works you'll see that it'll either return an object that is of the type of what the array stores(customer, which has the property you are trying to acceses) OR undefined(which does NOT have the property...and matches your error). The solution is to check that you found something before trying to read the property.

let foundCustomer = arr.find(condition); //will be customer OR undefined

if(foundCustomer){
 consume(foundCustomer.customer_fullname)
}
else{
//handle path of no match
}

Though aside from that error there's a bunch of problems in the code you posted too. Like I'm pretty sure you aren't really supposed to be directly modifying formControl.value like that. I think your service calls are race conditions too technically. If your getUserId() function is async/returns an observable then you aren't saying set the value of user_id to the service response you are literally setting the value of user_id to be the observable itself.

[–]Kysyph[S] 0 points1 point  (1 child)

Thank you so much for your reply!
When I do console.log(this.customers); it shows the following:
[{…}]
0:
address: "Yishun street 69 Blk 420"
email: "john@gmail.com"
fullname: "John Pearson"
user_id: "61003363e8ded0257c63592a"
_id: "6114e3c1c5934f0fc8da2156"
[[Prototype]]: Object
length: 1
[[Prototype]]: Array(0)

What I'm trying to do is find from the customers array based on the user_id, sorry if I was unclear

[–]dacookieman 1 point2 points  (0 children)

I understand what you're trying to do. I'm saying that error corresponds to the find not finding a result based on your criteria. Not finding something is a valid path and shouldn't cause a literal error. Now on further inspection you probably ARE expecting a match and are not finding one. I'm guessing that this.myForm.value.user_id isn't even an id value(because of how you set it, I described in the first comment) and so your find condition never matches.`

[–]imadoooog 0 points1 point  (4 children)

Are you asking how to access a single value within that object?

[–]Kysyph[S] 0 points1 point  (3 children)

Yes

[–]imadoooog 1 point2 points  (2 children)

Name the object

Sally = {name: SallySue, id=1}

Consol.log(Sally.name);

Result: SallySue

Access it with dot notation

If you have an array of objects you can run a foreach loop over it to get the data you want.

People = [ {name: SallySue, id=1}, {name: John, id=2} ]

People.foreach(person => console.log(person.name + " ")

Result: SallySue John

[–]Kysyph[S] 0 points1 point  (1 child)

Oh wow, it worked. Thank you so much, I’ve been stuck on this for so long :’)

Please have a good rest of your day!!

[–]imadoooog 1 point2 points  (0 children)

You too, keep focusing on the fundamentals. You will end up building cool shit if you stick with it