0

I am working with ionic 3 and I have problems with an alert controller when I try to push an element in mi array. I do not what is wrong with my code, I think that I only need to receive the parameters and push it to complete the action but I only get a big error when I try to execute my code.

I'm so sorry, I know that my English is so bad.

CODE

  addPregunta() {
    const prompt = this.alertCtrl.create({
      title: "Login",
      message: "Enter a name for this new album you're so keen on adding",
      inputs: [
        {
          name: "title",
          placeholder: "Title"
        }
      ],
      buttons: [
        {
          text: "Cancel",
          handler: data => {
            console.log("Cancel clicked");
          }
        },
        {
          text: "Save",
          handler: data => {
            const preObj = {
              type: "radio",
              label: data.title
            };
            this.preguntas.push(preObj);
            this.changeData(data.title);
            this.mermaidStart();
          }
        }
      ]
    });
    prompt.present();
  }

ARRAY

preguntas: object[];

ERROR

enter image description here

5
  • could you share the error Commented Feb 10, 2020 at 23:43
  • @C_Ogoo ok I edited the question Commented Feb 10, 2020 at 23:45
  • You are showing "ARRAY" preguntas: object[]; separately, without any context. It has to be defined in a scope in which the "CODE" can see it — so where exactly is it defined? Commented Feb 10, 2020 at 23:53
  • @C_Ogoo is preguntas: any[], sorry I had a mistake but the error is the same. And it is defined into the component at the top like another variable in angular/ionic. Commented Feb 10, 2020 at 23:56
  • @ASASCED, define your variable "preguntas" as preguntas:any[]=[] so preguntas is an empty array. if you define preguntas:any[], preguntas is null Commented Feb 11, 2020 at 7:32

2 Answers 2

1

preguntas: object[]; The preguntas property is defined but it's not initialised with a value.

console.log(this.preguntas) // will be undefined

The problem is in the save handler:

{
  text: "Save",
  handler: data => {
    const preObj = {
      type: "radio",
      label: data.title
    };
    this.preguntas.push(preObj); // <-- the problem is with this line
    this.changeData(data.title);
    this.mermaidStart();
}

When this.preguntas.push(preObj) is called for the first time. this.preguntas is undefined, array.push will not work because this.preguntas is not an array.

The options you have are to initialise the preguntas property as an array, or check the value in the handler before your call .push.

Option 1

Initialise the property as an array

preguntas: object[] = [];

Option 2 Check the value in the save handler before pushing.

There are countless ways to check or even use an immutable approach

// similar style with your existing code
if(this.preguntas) {
  this.preguntas.push(preObj);
} else {
  this.preguntas = [preObj];
}

// immutable approach
this.preguntas = [...this.preguntas, preObj]
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, its a simple error but yesterday I dindn't know what happend? :(
1

You have declared the variable as an Array type but you did not initialize it so fails when you are trying to push into it.

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.