0

I have a object creationParams declared outside of all my functions. The object will be manipulated throughout the functions. However, I'm having an issue in pushing an Object to creationParams's Property with an Array datatype.

Code

var creationParams = {};
creationParams.Project = {};
creationParams.Project.Workplaces = [];

$(#foo).click(function() { //this is the function that runs before someid
   //input validations here...

   creationParams.Org = $('#SelectedOrg').val();
   creationParams.Token = '';
});

$('#someid').click(function () {
    $(this).attr('href', '#');

    function isWpExisting(o) {
        for (var cntr = 0; cntr < creationParams.Project.Workplaces; cntr++) {
            if (creationParams.Project.Workplaces[cntr].Name.toUpperCase() === o) {
                return true;
            }
        }
        return false;
    }

    if ($('#wp-name').val() === '') {
        $('#wp-name').addClass("error-field").attr("data-original-title", "Please enter Workplace Name");
        $(this).attr('href', '#');
    }
    else if (isWpExisting($('#wp-name').val().toUpperCase()) === true && !$('#wp-wpcreate').attr('wp-edit')) {
        $('#wp-name').addClass("error-field").attr("data-original-title", "This Workplace is already existing");
        $(this).attr('href', '#');
    }
    else {
        $(this).attr('href', '#divWp-one');
        $('#wp-name').removeClass("error-field").removeAttr("data-original-title");
        var workPlace= {};
        workPlace.Name = $('#wp-name').val();
        workPlace.Description = $('#wp-description').val();

        if ($('#wp-wpcreate').attr('wp-edit')) {

            for (var t = 0; t < creationParams.Project.Workplaces.length; t++) {
                if (creationParams.Project.Workplaces[t].Name === $('#wp-wpcreate').attr('wp-edit')) {
                    creationParams.Project.Workplaces[t] = workPlace;
                    $('#wp-wpcreate').attr('wp-edit', '');
                }
            }
        }
        else {
            creationParams.Project.Workplaces.push(workPlace);
        }
    }
});

//
  ...some more functions here
//

Im getting an error of "Cannot read property 'push' of undefined". I just want my object to be pushed to the Array. I have tried making another array and pushing into it and it works.

Update: I tried console.log(creationParams.Project.Workplaces); and gave me undefined.

Update 2: I got it working by moving the complete initialization of the creationParams.Project.Workplaces in the #foo function. I wonder why though, what is the difference of initializing it in a different place?

Questions

  1. Why is it behaving like this?
  2. How can I make it work? Fixed (see Update 2)
Hexxed
  • 683
  • 1
  • 10
  • 28
  • 1
    post more code or an example snippet because it seems to work here – p u Jan 14 '19 at 04:56
  • 2
    Your issue is not replicated in the code you've provided. [Demo](http://jsfiddle.net/mfzcn8eu/) – Tyler Roper Jan 14 '19 at 04:57
  • 1
    https://stackoverflow.com/a/807908/5061290 – Ahmet Zeybek Jan 14 '19 at 04:59
  • Updated snippet, Thanks! – Hexxed Jan 14 '19 at 05:10
  • @AhmetZeybek yes, I am using input types. I can actually retrieve the values from the respective id's. – Hexxed Jan 14 '19 at 05:13
  • 1
    @Hexxed can you show console.log output of `creationParams.Project.Workplaces` in the else block where you're trying to push. also check type of`creationParams.Project.workPlaces`. i don't see any problem in code. may be somewhere you're manipulating workplaces and changing it to anything other than array – Code Maniac Jan 14 '19 at 05:23
  • @CodeManiac Weirdly enough. I'm getting `undefined` for it. But why though? – Hexxed Jan 14 '19 at 05:38
  • 1
    This can be a silly answer but try. I can see a small mistake in `isWpExisting` function. it should change like this `cntr < creationParams.Project.Workplaces.length`;. and try to define you global object like this. `var creationParams = { Project : { Workplaces : [] } }` – Dananjaya Ariyasena Jan 14 '19 at 05:49
  • Hi, thanks for the help guys. I got it working (*see update 2*). But im wondering why. – Hexxed Jan 14 '19 at 05:56

0 Answers0