Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON string value of "constructor" throws exception on .fromJS call #124

Open
scottksmith95 opened this issue Dec 31, 2012 · 2 comments · May be fixed by #173
Open

JSON string value of "constructor" throws exception on .fromJS call #124

scottksmith95 opened this issue Dec 31, 2012 · 2 comments · May be fixed by #173

Comments

@scottksmith95
Copy link

I am getting the following error when a data field value is the string "constructor":

Uncaught TypeError: Object function Object() { [native code] } has no method 'get' knockout.mapping-latest.debug.js:770

I am using version v2.3.2.

Here is my code to show how I am calling it:

var results = ko.observableArray();
ko.mapping.fromJS(data.items, {}, results);

data.items is an array of objects and the following is the JSON item that is causing this error:

{
    "id": 25602,
    "created": "2012-12-30T10:13:29.03",
    "created_text": "2012-12-30T18:13:29Z",
    "source_account": {
        "name": "Scott Smith",
        "display_name": "Scott Smith",
        "title": "Founder of coderbits",
        "location": "Northern California",
        "website_link": "http://scottksmith.com",
        "bio": "Scott Smith is the founder of coderbits.com, senior director of software development at Front Porch, Inc, and a scuba diving developer extraordinaire.",
        "id": 1,
        "views": 993,
        "created": "2012-08-24T11:41:43.157",
        "updated": "2012-12-29T20:20:55.24",
        "username": "scott",
        "gravatar_hash": "4400061d4ff1f220a9b5f060368cd869",
        "gravatar_link": "https://secure.gravatar.com/avatar/4400061d4ff1f220a9b5f060368cd869?d=https%3A%2F%2Fcoderbits.com%2Fimages%2Fgravatar.png&r=PG",
        "about": "Curently inhabiting Northern California, Scott Smith is a self described 'Founder of coderbits' with a proficiency in c#, javascript, web development, .net, and jquery.",
        "badge_total_score": 264,
        "show_summary": true,
        "show_skills": true,
        "show_code": true,
        "show_binaries": true,
        "show_designs": true,
        "show_education": true,
        "show_awards": true,
        "show_memberships": true,
        "show_activity_stream": true,
        "show_badges": true,
        "one_bit_badges": 48,
        "eight_bit_badges": 17,
        "sixteen_bit_badges": 5,
        "thirty_two_bit_badges": 0,
        "sixty_four_bit_badges": 0,
        "follower_count": 65,
        "following_count": 68,
        "top_skills": [
            {
                "id": 71711,
                "name": "c#",
                "count": 146
            },
            {
                "id": 71712,
                "name": "javascript",
                "count": 136
            },
            {
                "id": 71713,
                "name": "web development",
                "count": 132
            },
            {
                "id": 71714,
                "name": ".net",
                "count": 21
            },
            {
                "id": 71715,
                "name": "css",
                "count": 9
            }
        ],
        "top_tools": [
            {
                "id": 19360,
                "name": "visual studio",
                "count": 35
            },
            {
                "id": 19361,
                "name": "git",
                "count": 20
            },
            {
                "id": 19362,
                "name": "nuget",
                "count": 16
            }
        ],
        "top_interests": [
            {
                "id": 95584,
                "name": "javascript",
                "count": 208
            },
            {
                "id": 95585,
                "name": "c#",
                "count": 200
            },
            {
                "id": 95586,
                "name": "web development",
                "count": 140
            },
            {
                "id": 95587,
                "name": "visual studio",
                "count": 47
            },
            {
                "id": 95588,
                "name": ".net",
                "count": 42
            }
        ],
        "top_traits": [
            {
                "id": 84837,
                "name": "Quality",
                "count": 63
            },
            {
                "id": 84838,
                "name": "Productivity",
                "count": 52
            },
            {
                "id": 84839,
                "name": "Ambition",
                "count": 45
            },
            {
                "id": 84840,
                "name": "Knowledge",
                "count": 42
            },
            {
                "id": 84841,
                "name": "Self Development",
                "count": 30
            }
        ],
        "top_areas": [
            {
                "id": 52341,
                "name": "Code",
                "count": 141
            },
            {
                "id": 52342,
                "name": "Education",
                "count": 99
            },
            {
                "id": 52343,
                "name": "Memberships",
                "count": 13
            },
            {
                "id": 52344,
                "name": "Binaries",
                "count": 8
            },
            {
                "id": 52345,
                "name": "Designs",
                "count": 8
            }
        ]
    },
    "activity_type": {
        "id": 9,
        "value": "Invited",
        "icon_class": "icon-user",
        "display_text": " invited and signed up ",
        "dropdown": false
    },
    "target_account": {
        "name": "kriskd",
        "display_name": "kriskd",
        "title": "coder",
        "location": "planet earth",
        "website_link": "",
        "bio": "",
        "id": 4988,
        "views": 19,
        "created": "2012-12-30T10:13:28.817",
        "updated": "2012-12-30T13:02:31.993",
        "username": "kriskd",
        "gravatar_hash": "8c46482135d03742c0fcdce5e043bc94",
        "gravatar_link": "https://secure.gravatar.com/avatar/8c46482135d03742c0fcdce5e043bc94?d=https%3A%2F%2Fcoderbits.com%2Fimages%2Fgravatar.png&r=PG",
        "about": "Curently inhabiting planet earth, kriskd is a self described 'coder' with a proficiency in javascript, php, cakephp, constructor, and overwrite.",
        "badge_total_score": 45,
        "show_summary": true,
        "show_skills": true,
        "show_code": true,
        "show_binaries": false,
        "show_designs": false,
        "show_education": true,
        "show_awards": true,
        "show_memberships": true,
        "show_activity_stream": true,
        "show_badges": true,
        "one_bit_badges": 5,
        "eight_bit_badges": 3,
        "sixteen_bit_badges": 1,
        "thirty_two_bit_badges": 0,
        "sixty_four_bit_badges": 0,
        "follower_count": 1,
        "following_count": 1,
        "top_skills": [
            {
                "id": 74011,
                "name": "javascript",
                "count": 10
            },
            {
                "id": 74012,
                "name": "php",
                "count": 10
            },
            {
                "id": 74013,
                "name": "cakephp",
                "count": 1
            },
            {
                "id": 74014,
                "name": "constructor",
                "count": 1
            },
            {
                "id": 74015,
                "name": "overwrite",
                "count": 1
            }
        ],
        "top_tools": [
            {
                "id": 20013,
                "name": "git",
                "count": 2
            }
        ],
        "top_interests": [
            {
                "id": 98705,
                "name": "php",
                "count": 24
            },
            {
                "id": 98706,
                "name": "javascript",
                "count": 20
            },
            {
                "id": 98707,
                "name": "cakephp",
                "count": 11
            },
            {
                "id": 98708,
                "name": "css",
                "count": 10
            },
            {
                "id": 98709,
                "name": "drupal",
                "count": 10
            }
        ],
        "top_traits": [
            {
                "id": 87555,
                "name": "Communication",
                "count": 25
            },
            {
                "id": 87556,
                "name": "Influence",
                "count": 18
            },
            {
                "id": 87557,
                "name": "Knowledge",
                "count": 2
            },
            {
                "id": 87558,
                "name": "Productivity",
                "count": 1
            },
            {
                "id": 87559,
                "name": "Teamwork",
                "count": 1
            }
        ],
        "top_areas": [
            {
                "id": 53957,
                "name": "Code",
                "count": 6
            },
            {
                "id": 53958,
                "name": "Education",
                "count": 2
            },
            {
                "id": 53959,
                "name": "Memberships",
                "count": 2
            }
        ]
    },
    "external_account": null,
    "earned_badge": null,
    "input": null
}

The offending part of the JSON is this;

{
    "id": 74014,
    "name": "constructor",
    "count": 1
}

When I change the string "constructor" to anything else, it works just fine.

lawnsea added a commit to WaterfallEngineering/knockout.mapping that referenced this issue Sep 4, 2013
If a mapped observable has a value that is a property of `Object.prototype`,
`findBucket` returns that property's value instead of a `simpleObjectLookup`
instance.

Fix SteveSanderson#124
@lawnsea lawnsea linked a pull request Sep 4, 2013 that will close this issue
@lawnsea
Copy link
Contributor

lawnsea commented Sep 4, 2013

We hit this as well: the value 'watch' crashes on Firefox.

@RobertY9
Copy link

Still happening in current version

var originalData = { id: 1, name: "watch" }; var viewModel = ko.mapping.fromJS(originalData);

crissdev pushed a commit to crissdev/knockout.mapping that referenced this issue Feb 13, 2015
…ence

If a mapped observable has a value that is a property of `Object.prototype`,
`findBucket` returns that property's value instead of a `simpleObjectLookup`
instance.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants