1

How can I access the value exist from an array? I think I didn't pass the array inside? Any help or advice

var isExist = this.props.isFavorite(this.props.code);
console.log(isExist)

I have this variable isExist containing the response from console below.

[]
 client: [id: "LvR05w9v9xrC3r4V1W8g", exist: true]
 length: 1
 _proto_:Array(0)

How can I access the exist in my array? When I tried isExist[0].exist I'm getting an error. Any help?

isExist.exist = Undefined 
isExist[0].exist = TypeError: Cannot read property 'exist' of undefined

favorite method where I am accessing and pushing data to the array

export const isFavorite = (data) => dispatch => {
  let exist = []; 

  var clientQuery = firebase.firestore().collection(path).where('client_id', '==', data);
  clientQuery.get().then((querySnapshot) => {
    querySnapshot.forEach((doc) => {
        var data = [];
        data.id = doc.id;
        data.exist = doc.exists;
        exist.push(data)
     });
 });
 return exist;
}
1
  • What is the data type of isExist variable? Commented Oct 27, 2018 at 6:07

3 Answers 3

1

isFavorite returns a function which takes one argument dispatch and returns exist array. You seem to use async code to populate exist array. So when that function returns exist is an empty array []. You either need to continue using promises or use await. And you need to call the function returned by isFavorite.

If this.props.isFavorite and const isFavorite are not the same then add the code for this.props.isFavorite please.

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

Comments

0

You're creating an array Object. Then the array object {data}[]. So the problem is, the data is actually not only an array but also an object.

Try doing this.

var data;
data.id = doc.id;
data.exist = doc.exist;
exist.push(data);

Now you will have exist data that would be an array of Object. Then iterate from it.

 exist[0].id;
 //or try 
 exist[0].data.id; 
 //Depends on how you implement your data.

3 Comments

Hi Aizen, index.js:50 Uncaught (in promise) TypeError: Cannot set property 'id' of undefined
You don't need temp variable. exist.push({id: doc.id, exist: doc.exists})
Ujin, you're correct Ujin you don't need to. But Jay Ar is new to OOP can't you tell? Remember Ujin, when answering questions. You need to make sure to adapt to their way of coding instead letting them adapt to yours. Answer with their own templates instead of remaking them on your own best practices.
0

Since client array doesn’t contain object with keys and values I would recommend you to try with array of index with split() to get id value and exist value from array like

Like

  var isExist = this.props.isFavorite(this.props.code);
  var id = isExist.client[0];
  var exist = isExist.client[1];
  var idValue = id ? id.split(': '): '';
  console.log(idValue);
  const existValue = exist ? exist.split(': '): false;
  console.log(existValue);

And here change data = []; array to data ={}; object

   querySnapshot.forEach((doc) => {
    var data = {};
    data.id = doc.id;
    data.exist = doc.exists;
    exist.push(data)
 });

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.