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

hasOwnProperty is not a function #89

Closed
lucianobosco opened this issue Nov 28, 2022 · 3 comments
Closed

hasOwnProperty is not a function #89

lucianobosco opened this issue Nov 28, 2022 · 3 comments

Comments

@lucianobosco
Copy link

Since last update I'm getting hasOwnProperty is not a function
I used to create a payload with differences to be sent to my API through axios:

      const diffs = diff(originalResource.value, resource.value)
      const payload = {
        ...(diffs.hasOwnProperty('name') && { name: resource.value.name }),
        ...(diffs.hasOwnProperty('slug') && { slug: resource.value.slug }),
        ...(diffs.hasOwnProperty('visible') && { visible: resource.value.visible }),
        ...(diffs.hasOwnProperty('postCategory') && { post_category_id: resource.value.postCategory?.id || null }),
        ...(diffs.hasOwnProperty('parent') && { parent_id: resource.value.parent?.id || null })
      }

Did anything change in regards to returned response from method diff(). In that case, how can I get diff() properties changed in order to create a new payload object?

@mattphillips
Copy link
Owner

Hi @lucianobosco due to a potential vulnerability with unsafely setting the diffed properties on the returned diff object, the diffed object is now created using Object.create(null). This means that the returned diffed object has no prototype and so hasOwnProperty will be undefined.

In your case you could spread the diff object into another object with the default prototype functions available.

      const diffs = diff(originalResource.value, resource.value)
      const d = { ...diffs }
      const payload = {
        ...(d.hasOwnProperty('name') && { name: resource.value.name }),
        ...(d.hasOwnProperty('slug') && { slug: resource.value.slug }),
        ...(d.hasOwnProperty('visible') && { visible: resource.value.visible }),
        ...(d.hasOwnProperty('postCategory') && { post_category_id: resource.value.postCategory?.id || null }),
        ...(d.hasOwnProperty('parent') && { parent_id: resource.value.parent?.id || null })
      }

@lucianobosco
Copy link
Author

Hi @lucianobosco due to a potential vulnerability with unsafely setting the diffed properties on the returned diff object, the diffed object is now created using Object.create(null). This means that the returned diffed object has no prototype and so hasOwnProperty will be undefined.

In your case you could spread the diff object into another object with the default prototype functions available.

      const diffs = diff(originalResource.value, resource.value)
      const d = { ...diffs }
      const payload = {
        ...(d.hasOwnProperty('name') && { name: resource.value.name }),
        ...(d.hasOwnProperty('slug') && { slug: resource.value.slug }),
        ...(d.hasOwnProperty('visible') && { visible: resource.value.visible }),
        ...(d.hasOwnProperty('postCategory') && { post_category_id: resource.value.postCategory?.id || null }),
        ...(d.hasOwnProperty('parent') && { parent_id: resource.value.parent?.id || null })
      }

Thanks a lot! That works

@JuanJo4
Copy link

JuanJo4 commented Apr 19, 2023

Note that inner objects doesn't have prototype too, so even if you spread the object you might face the same hasOwnProperty is not a function error.

const diffs = diff(originalResource.value, resource.value)
const d = { ...diffs }

d.innerObj.hasOwnProperty('myProp') // will throw hasOwnProperty is not a function

A different solution than spreading the diff object could be to use Object.hasOwnProperty.call directly, like this:

Object.hasOwnProperty.call(diffs, prop)

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

No branches or pull requests

3 participants