question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Passing Context objects through express does not work

See original GitHub issue

I’ve been having an extended discussion on StackOverflow (link) with @sethkinast regarding passing Context objects through Express.

I have put together a very quick example of how to replicate this issue here: https://github.com/adamkdean/dust-test

But to save time, the script is this:

const path = require('path')
const hoffman = require('hoffman')
const express = require('express')
const request = require('request')
const duster = require('duster')
const dust = duster.dust

const app = express()
const base = dust.context({
  isBase: true,
  age: 26,
  people: [
    { name: 'exampleA' },
    { name: 'exampleB' }
  ]
})

app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'dust')
app.engine('dust', hoffman.__express())

app.get('/', function (req, res) {
  res.render('people', {})
})

app.get('/pojo', function (req, res) {
  res.render('people', {
    age: 26,
    people: [
      { name: 'exampleA' },
      { name: 'exampleB' }
    ]
  })
})

app.get('/context', function (req, res) {
  res.render('people', base.push({
    people: [
      { name: 'adam' }
    ]
  }))
})

app.listen(4000, function () {
  console.log('Visit http://localhost:4000 to see streaming!')
})

If you go to /pojo then the plain old JavaScript object renders fine (the template is {#people}<li>{name} aged {age}</li>{/people}), but if you go to /context then it doesn’t render fine at all.

I think this would probably be a better place to discuss the issue.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
sethkinastcommented, Sep 10, 2016

Here’s the issue: https://github.com/expressjs/express/blob/master/lib/application.js#L535

A new object, renderOptions, gets created. This object of course doesn’t have Context as its prototype so Dust tries to shove it into a context.

It’s very interesting that no one has ever run into this before. I would never have expected Express to mangle a context object passed to it. We can fix this by dropping the instanceof check on our end.

0reactions
sethkinastcommented, Sep 13, 2016

This is available as 2.7.4.

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - Passing context implicitly across functions and ...
If i pass the context as a variable i have to refactor a lof of my code base and a lot of business...
Read more >
apollo-server-testing context is not receiving the `req` object
The test relies on the req object being passed to the context function in the server setup, but I'm only getting an empty...
Read more >
Context-aware logging in Node.js - LogRocket Blog
The issue could be fixed by passing the “context” argument to the createUser function, but that wouldn't solve anything. What if there was ......
Read more >
Koa - next generation web framework for node.js
A Koa Context encapsulates node's request and response objects into a single object which provides many helpful methods for writing web applications and...
Read more >
AWS Lambda context object in Node.js
AWS Lambda context object in Node.js. When Lambda runs your function, it passes a context object to the handler. This object provides methods...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found