0
  • I have searched here for an answer to this question with no luck.

I have an observable array which contains only one entry:

Array

I have it stored in self.user()

POSData.Users.getByEmail(sEmail)
        .then(data => {
        //console.log(data)
        self.user.push(data);
    })

Now I simply want to extract a few values and assign them to their own observables, BUT... I can't.

I have tried the following to get the firstName...

console.dir(self.user());

    //console.log(self.user()[0].data.firstName());
    //console.log(self.user().firstName());
    //console.log(self.user().data.firstName());
    //console.log(self.user()[0].data.firstName());
    //console.log(self.user().data[1].firstName());


Does anyone know how to drill down and get to the information I want?

Thanks for looking.

John

3
  • try doing a ko.toJS(self.user()) to see what structure you actually end up with after pushing the info. Commented Oct 10, 2019 at 22:30
  • it's empty apparently.... what in the world?!?! How can that be, when I console.log self.user() it shows the data there. Commented Oct 10, 2019 at 22:34
  • can you put it in a fiddle (doesn't need knockout I just want to see the code in context) Commented Oct 10, 2019 at 22:43

1 Answer 1

3

You're storing the raw data you got back from your service into your array. You should access members of that data in that form. The firstName property is not an observable, it's just a string in the data property so you shouldn't be calling it as if it was an observable. The only observable in your example is apparently self.user.

Based on your screenshot your new data looks something like this:

{
  data: {
    firstName: 'John',
    lastName: 'Smith'
  },
  message: 'User retrieved successfully',
  status: null
}

If you want to get the first name of this object in your user array, you'd access it like this:

self.user()[0].data.firstName
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.