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

obj.hasOwnProperty undefined is not a function nodejs #468

Closed
fotisp opened this issue Jun 24, 2015 · 9 comments · Fixed by #1017
Closed

obj.hasOwnProperty undefined is not a function nodejs #468

fotisp opened this issue Jun 24, 2015 · 9 comments · Fixed by #1017

Comments

@fotisp
Copy link

fotisp commented Jun 24, 2015

Hello,

I was noticing some discrepancies in some of the rendering of the nunjuck templates. After enough digging around I boiled it down to different nodejs versions between development and production. However the production machine has a more resent version of nodejs.

~ > node --version
v0.12.2

With version v0.12.x of node, this line

https://github.com/mozilla/nunjucks/blob/master/src/lib.js#L271

fails with TypeError: undefined is not a function.

Has anyone encountered this, is there any know workaround?

@fotisp
Copy link
Author

fotisp commented Jun 24, 2015

Sorry,

An example that causes problems is

        {% for key,value in extra  %}
          <input type="hidden" name="{{ key  }}" value="{{ value }}" />
        {% endfor %}

Where extra is just a JSON object.

@ghost
Copy link

ghost commented Jun 24, 2015

@fotisp What do you mean that extra is a JSON object?

I tried the following in node 0.12 and it works fine:

var extra = require('./extra.json');
console.log(extra.hasOwnProperty('key'));

and the output is true.

This also shows that extra has a working hasOwnProperty method:

var extra = JSON.parse('{"key": "val"}');
console.log(extra.hasOwnProperty('key'));

@devoidfury
Copy link
Contributor

Does the JSON object you've got have a property hasOwnProperty? That's the only way I could reproduce that error, TypeError: undefined is not a function.

extra = { hasOwnProperty: undefined }

Could you provide a full test case that produces the error?

@devoidfury
Copy link
Contributor

This could also happen if the object does not have Object in it's prototype chain, for example:

extra = Object.create(null)

The safer way to handle this case would be to change https://github.com/mozilla/nunjucks/blob/master/src/lib.js#L271 to this:

 if(Object.prototype.hasOwnProperty.call(obj, k)) {

--- the only issue with that is that it may break some older browser compatibility, so we'd need to add extra logic for that.

@jlongster
Copy link
Contributor

@fotisp, can you check out @devoidfury's comments above?

@fotisp
Copy link
Author

fotisp commented Aug 28, 2015

Hi @jlongster,

I will try to take a look during the weekend and report back.

@xtr0
Copy link

xtr0 commented Oct 11, 2015

This could also happen if the object does not have Object in it's prototype chain, for example:

extra = Object.create(null)

Please note: For example in express.js res.locals is created in exactly this way intentionally. So if one is using res.locals in renderString as a context there is an issue with migration to nunjucks 2+ (yeah, it is possible to replace res.locals with something like _.extend({}, res.locals), but...)

(and sorry for my English)

@carljm
Copy link
Contributor

carljm commented Dec 14, 2015

I'm not convinced this is worth fixing, but if someone wanted to look into the browser-compatibility implications of the Object.prototype.hasOwnProperty.call(...) solution suggested above, I'd consider it.

@asprouse
Copy link
Contributor

asprouse commented Dec 1, 2016

@carljm This is enough of a problem that ESLint has a rule about it. http://eslint.org/docs/rules/no-prototype-builtins This setting was enabled in v4 of Airbnb's widely (43k+ stars) used ESLint config. I have noticed some libraries, notably graphql-js, creating objects with Object.create(null);passing them into nunjucks throws this error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants