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

Cant validate #each problem #1820

Closed
duard opened this issue Jan 18, 2022 · 4 comments
Closed

Cant validate #each problem #1820

duard opened this issue Jan 18, 2022 · 4 comments

Comments

@duard
Copy link

duard commented Jan 18, 2022

I'm not capable of validate all variables inside my template when using #each.
here is the jsfidle : https://jsfiddle.net/why2t30d/

here is the error :

VM296:4 Uncaught TypeError: Cannot read properties of undefined (reading 'deliveryAddress')
    at eval (eval at createFunctionContext (handlebars.js:3562:24), <anonymous>:4:79)
    at prog (handlebars.js:1206:13)
    at execIteration (handlebars.js:677:20)
    at Object.<anonymous> (handlebars.js:687:14)
    at Object.eval [as main] (eval at createFunctionContext (handlebars.js:3562:24), <anonymous>:6:31)
    at main (handlebars.js:1160:33)
    at ret (handlebars.js:1163:13)
    at ret (handlebars.js:3258:22)
    at ?editor_console=true:142:47

Thanks for any help.

@jaylinski
Copy link
Member

This seems to be a bug with strict-mode.

You can either not use strict-mode, or access your variables without a block param:

{{#each data.deliveries}}
  {{deliveryAddress.street}},
  {{deliveryAddress.number}}
  -
  {{deliveryAddress.streetComplement}}<br />
  {{deliveryAddress.neighborhood}}
  -
  {{deliveryAddress.city}},
  {{deliveryAddress.state}}
  -
  {{deliveryAddress.country}}.<br />
  CEP:
  {{deliveryAddress.zipCode}}<br />
  Ponto de referência: Não informado.
{{/each}}

@duard
Copy link
Author

duard commented Jan 18, 2022

Here is another case : https://jsfiddle.net/duardbr/v38bsqf5/1/

Error below

VM131:5 Uncaught TypeError: Cannot read properties of undefined (reading 'items')
    at eval (eval at createFunctionContext (handlebars.js:3562:24), <anonymous>:5:96)
    at prog (handlebars.js:1206:13)
    at execIteration (handlebars.js:677:20)
    at Object.<anonymous> (handlebars.js:687:14)
    at Object.eval [as main] (eval at createFunctionContext (handlebars.js:3562:24), <anonymous>:6:31)
    at main (handlebars.js:1160:33)
    at ret (handlebars.js:1163:13)
    at ret (handlebars.js:3258:22)
    at ?editor_console=true:136:47

This seems to be a bug with strict-mode.

You can either not use strict-mode, or access your variables without a block param:

{{#each data.deliveries}}
  {{deliveryAddress.street}},
  {{deliveryAddress.number}}
  -
  {{deliveryAddress.streetComplement}}<br />
  {{deliveryAddress.neighborhood}}
  -
  {{deliveryAddress.city}},
  {{deliveryAddress.state}}
  -
  {{deliveryAddress.country}}.<br />
  CEP:
  {{deliveryAddress.zipCode}}<br />
  Ponto de referência: Não informado.
{{/each}}

@jandppw
Copy link

jandppw commented Feb 8, 2022

This took me hours to find!

The examples of the documentation even fail:

/* eslint-env mocha */
const testName = require('../../_testName')
const Handlebars = require('handlebars')

// https://handlebarsjs.com/examples/builtin-helper-with-block-param.html
const template = `{{#with city as | city |}}
  {{#with city.location as | loc |}}
    {{city.name}}: {{loc.north}} {{loc.east}}
  {{/with}}
{{/with}}`
const context = {
  city: {
    name: 'San Francisco',
    summary: 'San Francisco is the <b>cultural center</b> of <b>Northern California</b>',
    location: {
      north: '37.73,',
      east: -122.44
    },
    population: 883305
  }
}

describe(testName(module), function () {
  it('fails the with example from the documentation, with strict', async function () {
    const handlebar = Handlebars.create()
    const compiled = handlebar.compile(template, { strict: true })
    compiled.bind(undefined, context).should.throw()
  })
  it('works as expected with the with example from the documentation, without strict', async function () {
    const handlebar = Handlebars.create()
    const compiled = handlebar.compile(template, { strict: false })
    const result = compiled(context)
    console.log(result)
    result.should.be.a.String()
  })
})

@jandppw
Copy link

jandppw commented Feb 8, 2022

It also fails with assumeObjects: true

  it('works as expected with the with example from the documentation, without strict', async function () {
    const handlebar = Handlebars.create()
    const compiled = handlebar.compile(template, { strict: false, assumeObjects: true })
    compiled.bind(undefined, context).should.throw()
  })

jaylinski pushed a commit to arturmakaranka/handlebars.js that referenced this issue May 25, 2022
jaylinski pushed a commit that referenced this issue May 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants