{"id":1936,"date":"2022-08-30T15:20:41","date_gmt":"2022-08-30T15:20:41","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/05\/mvc4-webapi-knockout-json-invalid-operand-to-in-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:20:41","modified_gmt":"2022-08-30T15:20:41","slug":"mvc4-webapi-knockout-json-invalid-operand-to-in-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/mvc4-webapi-knockout-json-invalid-operand-to-in-collection-of-common-programming-errors\/","title":{"rendered":"MVC4 WebApi Knockout JSON Invalid operand to &#39;in&#39;-Collection of common programming errors"},"content":{"rendered":"<p>Hy, I&#8217;m stuck with this error message and I can not find an solution.<\/p>\n<p>I get this message error in the Knockout JavaScript library v2.2.0:<\/p>\n<blockquote>\n<p>Unhandled exception at line 1053, column 5 in localhost:port\/Scripts\/knockout-2.2.0.debug.js 0x800a138f &#8211; Microsoft JScript runtime error: Invalid operand to &#8216;in&#8217;: Object expected If there is a handler for this exception, the program may be safely continued.<\/p>\n<\/blockquote>\n<p>It stops at this line of code in knockout-2.2.0.debug.js<\/p>\n<pre><code> if ((initialValues !== null) &amp;&amp; (initialValues !== undefined) &amp;&amp; !('length' in initialValues)) \n<\/code><\/pre>\n<p>I use this WebApi:<\/p>\n<pre><code>public class ProductsController : ApiController\n{\n  IEnumerable products = new List() \n    { \n        new Product { Id = 1, Name = \"Tomato_Soup\", Category = \"Groceries\", Price = 1 }, \n        new Product { Id = 2, Name = \"Yo-yo\", Category = \"Toys\", Price = 3.75M }, \n        new Product { Id = 3, Name = \"Hammer\", Category = \"Hardware\", Price = 16.99M } \n    };\n\n      public IEnumerable GetAllProducts(){\n            return products.AsEnumerable();    }\n<\/code><\/pre>\n<p>The scripts that I use are in a header section<\/p>\n<pre><code>@section Testscripts\n{\n    \n     \n\n\n}\n<\/code><\/pre>\n<p>And the Knockout code in the footer default script section<\/p>\n<pre><code>@section scripts\n{\n          \n        var apiUrl = '@Url.RouteUrl(\"DefaultApi\", new { httproute = \"\", controller = \"products\" })';  \n\n        function Product(data) {            \n            this.Id = ko.observable(data.Id);\n            this.Name = ko.observable(data.Name);\n            this.Price = ko.observableArray(data.Price);\n            this.Category = ko.observable(data.Category);\n\n        }\n\n        function ProductViewModel() {\n\n            var self = this;\n            self.myproducts = ko.observableArray([]);\n\n\n        $.getJSON(apiUrl, function (allData) {\n            var mappedProducts = $.map(allData, function (item) { return new Product(item) });\n\n            self.myproducts(mappedProducts);\n\n        });\n      };\n   ko.applyBindings(new ProductViewModel);\n}\n<\/code><\/pre>\n<p>and show the data in body:<\/p>\n<ul data-bind=\"foreach: myproducts\"><\/ul>\n<pre>\n<\/pre>\n<ol>\n<li>\n<p><code>The bug is in your <code>Product<\/code> function.<\/code><\/p>\n<p>You want to create an <code>ko.observableArray<\/code> from <code>data.Price<\/code> which is a decimal value and not an array of values, which results in this not so nice exception.<\/p>\n<p>Change to <code>ko.observable<\/code> and it should work:<\/p>\n<pre><code>function Product(data) {            \n        this.Id = ko.observable(data.Id);\n        this.Name = ko.observable(data.Name);\n        this.Price = ko.observable(data.Price);\n        this.Category = ko.observable(data.Category);\n\n    }\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-12-05 09:40:32. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Hy, I&#8217;m stuck with this error message and I can not find an solution. I get this message error in the Knockout JavaScript library v2.2.0: Unhandled exception at line 1053, column 5 in localhost:port\/Scripts\/knockout-2.2.0.debug.js 0x800a138f &#8211; Microsoft JScript runtime error: Invalid operand to &#8216;in&#8217;: Object expected If there is a handler for this exception, the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1936","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1936","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1936"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1936\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1936"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1936"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1936"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}