0

I am very new to knockoutjs I got the error "The argument passed when initializing an observable array must be an array,or null,or undefined.I don't have an idea how to solve it.Can any one help me please.

 viewModel = function ()
 {
    var self = this;
    self.rows = ko.observableArray([]);
    self.items = ko.observableArray(childData.items);
    self.otherCharges = ko.observableArray(childData.otherCharges);
    self.add = function (type) {

        var entry = new Entry({});
         entry.SNo(VMRows().length);
        entry.Type(type);
        self.rows.push(entry);

    };

    self.remove = function (entry) {
        self.rows.remove(entry);
        i = 0;
        ko.utils.arrayForEach(VMRows, function (e) {
            e.SNo(i++);
        });
    };

};
VM[container] = new viewModel();

var VMRows = VM[container].rows;
1
  • Beware that your code, as quoted, is falling prey to The Horror of Implicit Globals (that's a post on my anemic little blog). Declare your variables (such as viewModel). Commented Aug 11, 2017 at 6:58

1 Answer 1

3

The message is very clear: You're passing a value into ko.observableArray that isn't an array, null, or undefined. So that tells us that either childData.items or childData.otherCharges is not an array, not null, and not undefined. At least one of them is some other value. (Obviously I can't tell you which; you haven't show us what childData is.)

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

3 Comments

Hi,T.J.Crowder <script> var childData={items:@Html.Raw(Json.Encode(Model.xShiftA)), otherCharges:@Html.Raw(Json.Encode(Model.xShiftB)), rows:@Html.Raw(Json.Encode(Model.xDetails))}; </script> is my script and in my project I have to you two knockoutjs lists for shiftA&B but no items were displayed when I click Edit
@Sowjanya: Quoting server-side templating code in a comment is not useful. Show what the browser sees, not the server code, using the "edit" link on the question. Now I can' thelp because I don't know what Model.xShiftA and Model.xShiftB are. (And I don't need to know. I just need to know what the browser sees.)
Hi,T.J.Crowder,thank you for your valuable answer.As u did I have declared some of the elements of child data as ko.observableArray which are not array elements.How I removed Array from ko.observableArray so my screen is working

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.