Array adds undefined element, angular 6












1















I have a problem with an array that is supposed to be empty, the problem is that it is not, and it has an undefined element inside so it does not allow me to query the backend correctly.



this is the component with problem and the console log:



export class AvailabilityVerifierComponent implements OnInit {

usersAvailabilities: UserAvailability = ;

users: Array<User> = ;

@Input()
set userToVerify(user: User){
console.log('users array in input');
console.log(this.users);
console.log('User en AvailabilityVerifierComponent');
console.log(user);
console.log('users componente after push ');
console.log(this.users);
this.users.push(user);
console.log('users componente before push ');
console.log(this.users);
this.verifyAvailabilitiesService.verify(this.users)
.subscribe(
response => {
this.usersAvailabilities = response;
},
error => {
console.log(error);
}
);

}

constructor(
private verifyAvailabilitiesService: VerifyAvailabilitiesService
) { }

ngOnInit() {
}
}


enter image description here










share|improve this question


















  • 2





    change users: Array<User> = in users: User; in the onInit -> this.users = ; and do a console.log(this.user). Is empty?

    – Jacopo Sciampi
    Nov 21 '18 at 15:37











  • now is empty, cool. but angular now it shows me an error when executing the input function because it can not use the .push method in undefined. After I add a user this is solved, but before that the error is present.

    – kmilo93sd
    Nov 21 '18 at 15:45
















1















I have a problem with an array that is supposed to be empty, the problem is that it is not, and it has an undefined element inside so it does not allow me to query the backend correctly.



this is the component with problem and the console log:



export class AvailabilityVerifierComponent implements OnInit {

usersAvailabilities: UserAvailability = ;

users: Array<User> = ;

@Input()
set userToVerify(user: User){
console.log('users array in input');
console.log(this.users);
console.log('User en AvailabilityVerifierComponent');
console.log(user);
console.log('users componente after push ');
console.log(this.users);
this.users.push(user);
console.log('users componente before push ');
console.log(this.users);
this.verifyAvailabilitiesService.verify(this.users)
.subscribe(
response => {
this.usersAvailabilities = response;
},
error => {
console.log(error);
}
);

}

constructor(
private verifyAvailabilitiesService: VerifyAvailabilitiesService
) { }

ngOnInit() {
}
}


enter image description here










share|improve this question


















  • 2





    change users: Array<User> = in users: User; in the onInit -> this.users = ; and do a console.log(this.user). Is empty?

    – Jacopo Sciampi
    Nov 21 '18 at 15:37











  • now is empty, cool. but angular now it shows me an error when executing the input function because it can not use the .push method in undefined. After I add a user this is solved, but before that the error is present.

    – kmilo93sd
    Nov 21 '18 at 15:45














1












1








1








I have a problem with an array that is supposed to be empty, the problem is that it is not, and it has an undefined element inside so it does not allow me to query the backend correctly.



this is the component with problem and the console log:



export class AvailabilityVerifierComponent implements OnInit {

usersAvailabilities: UserAvailability = ;

users: Array<User> = ;

@Input()
set userToVerify(user: User){
console.log('users array in input');
console.log(this.users);
console.log('User en AvailabilityVerifierComponent');
console.log(user);
console.log('users componente after push ');
console.log(this.users);
this.users.push(user);
console.log('users componente before push ');
console.log(this.users);
this.verifyAvailabilitiesService.verify(this.users)
.subscribe(
response => {
this.usersAvailabilities = response;
},
error => {
console.log(error);
}
);

}

constructor(
private verifyAvailabilitiesService: VerifyAvailabilitiesService
) { }

ngOnInit() {
}
}


enter image description here










share|improve this question














I have a problem with an array that is supposed to be empty, the problem is that it is not, and it has an undefined element inside so it does not allow me to query the backend correctly.



this is the component with problem and the console log:



export class AvailabilityVerifierComponent implements OnInit {

usersAvailabilities: UserAvailability = ;

users: Array<User> = ;

@Input()
set userToVerify(user: User){
console.log('users array in input');
console.log(this.users);
console.log('User en AvailabilityVerifierComponent');
console.log(user);
console.log('users componente after push ');
console.log(this.users);
this.users.push(user);
console.log('users componente before push ');
console.log(this.users);
this.verifyAvailabilitiesService.verify(this.users)
.subscribe(
response => {
this.usersAvailabilities = response;
},
error => {
console.log(error);
}
);

}

constructor(
private verifyAvailabilitiesService: VerifyAvailabilitiesService
) { }

ngOnInit() {
}
}


enter image description here







arrays angular angular6






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 15:34









kmilo93sdkmilo93sd

89110




89110








  • 2





    change users: Array<User> = in users: User; in the onInit -> this.users = ; and do a console.log(this.user). Is empty?

    – Jacopo Sciampi
    Nov 21 '18 at 15:37











  • now is empty, cool. but angular now it shows me an error when executing the input function because it can not use the .push method in undefined. After I add a user this is solved, but before that the error is present.

    – kmilo93sd
    Nov 21 '18 at 15:45














  • 2





    change users: Array<User> = in users: User; in the onInit -> this.users = ; and do a console.log(this.user). Is empty?

    – Jacopo Sciampi
    Nov 21 '18 at 15:37











  • now is empty, cool. but angular now it shows me an error when executing the input function because it can not use the .push method in undefined. After I add a user this is solved, but before that the error is present.

    – kmilo93sd
    Nov 21 '18 at 15:45








2




2





change users: Array<User> = in users: User; in the onInit -> this.users = ; and do a console.log(this.user). Is empty?

– Jacopo Sciampi
Nov 21 '18 at 15:37





change users: Array<User> = in users: User; in the onInit -> this.users = ; and do a console.log(this.user). Is empty?

– Jacopo Sciampi
Nov 21 '18 at 15:37













now is empty, cool. but angular now it shows me an error when executing the input function because it can not use the .push method in undefined. After I add a user this is solved, but before that the error is present.

– kmilo93sd
Nov 21 '18 at 15:45





now is empty, cool. but angular now it shows me an error when executing the input function because it can not use the .push method in undefined. After I add a user this is solved, but before that the error is present.

– kmilo93sd
Nov 21 '18 at 15:45












1 Answer
1






active

oldest

votes


















4














Your console log sadly changes over time; it gets revaluated. There is no way your initial array has one undefined element in it. What happens here is that when your component is initialized, the input is called with undefined as a parameter. Thus you push "undefined" into your array. Change the this.users.push(user) into



if (user) { 
this.users.push(user);
}


and you should be all set; except your server will be called twice. You might just want to add:



if (!user) {
return;
}


to the top of your userToVerify method.






share|improve this answer
























  • this has solved the problem correctly, thank you very much for the help.

    – kmilo93sd
    Nov 21 '18 at 15:50











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415491%2farray-adds-undefined-element-angular-6%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














Your console log sadly changes over time; it gets revaluated. There is no way your initial array has one undefined element in it. What happens here is that when your component is initialized, the input is called with undefined as a parameter. Thus you push "undefined" into your array. Change the this.users.push(user) into



if (user) { 
this.users.push(user);
}


and you should be all set; except your server will be called twice. You might just want to add:



if (!user) {
return;
}


to the top of your userToVerify method.






share|improve this answer
























  • this has solved the problem correctly, thank you very much for the help.

    – kmilo93sd
    Nov 21 '18 at 15:50
















4














Your console log sadly changes over time; it gets revaluated. There is no way your initial array has one undefined element in it. What happens here is that when your component is initialized, the input is called with undefined as a parameter. Thus you push "undefined" into your array. Change the this.users.push(user) into



if (user) { 
this.users.push(user);
}


and you should be all set; except your server will be called twice. You might just want to add:



if (!user) {
return;
}


to the top of your userToVerify method.






share|improve this answer
























  • this has solved the problem correctly, thank you very much for the help.

    – kmilo93sd
    Nov 21 '18 at 15:50














4












4








4







Your console log sadly changes over time; it gets revaluated. There is no way your initial array has one undefined element in it. What happens here is that when your component is initialized, the input is called with undefined as a parameter. Thus you push "undefined" into your array. Change the this.users.push(user) into



if (user) { 
this.users.push(user);
}


and you should be all set; except your server will be called twice. You might just want to add:



if (!user) {
return;
}


to the top of your userToVerify method.






share|improve this answer













Your console log sadly changes over time; it gets revaluated. There is no way your initial array has one undefined element in it. What happens here is that when your component is initialized, the input is called with undefined as a parameter. Thus you push "undefined" into your array. Change the this.users.push(user) into



if (user) { 
this.users.push(user);
}


and you should be all set; except your server will be called twice. You might just want to add:



if (!user) {
return;
}


to the top of your userToVerify method.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 15:45









ArneArne

551212




551212













  • this has solved the problem correctly, thank you very much for the help.

    – kmilo93sd
    Nov 21 '18 at 15:50



















  • this has solved the problem correctly, thank you very much for the help.

    – kmilo93sd
    Nov 21 '18 at 15:50

















this has solved the problem correctly, thank you very much for the help.

– kmilo93sd
Nov 21 '18 at 15:50





this has solved the problem correctly, thank you very much for the help.

– kmilo93sd
Nov 21 '18 at 15:50


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415491%2farray-adds-undefined-element-angular-6%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy