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

Using Nuxt as a middleware : TypeError: Cannot read property 'renderer' of undefined #18

Closed
VincentLoy opened this issue Nov 28, 2016 · 3 comments
Labels

Comments

@VincentLoy
Copy link

VincentLoy commented Nov 28, 2016

Hello there, I'm trying to create a really basic app using Nuxt as a middleware and I got an issue when I try to access the server.
Here is the complete error message:
The message appear both in the browser and the terminal

TypeError: Cannot read property 'renderer' of undefined
   at render (/home/vincent/WebstormProjects/nuxtExpress/node_modules/nuxt/lib/nuxt.js:94:14)
   at Layer.handle [as handle_request] (/home/vincent/WebstormProjects/nuxtExpress/node_modules/express/lib/router/layer.js:95:5)
   at trim_prefix (/home/vincent/WebstormProjects/nuxtExpress/node_modules/express/lib/router/index.js:312:13)
   at /home/vincent/WebstormProjects/nuxtExpress/node_modules/express/lib/router/index.js:280:7
   at Function.process_params (/home/vincent/WebstormProjects/nuxtExpress/node_modules/express/lib/router/index.js:330:12)
   at next (/home/vincent/WebstormProjects/nuxtExpress/node_modules/express/lib/router/index.js:271:10)
   at expressInit (/home/vincent/WebstormProjects/nuxtExpress/node_modules/express/lib/middleware/init.js:33:5)
   at Layer.handle [as handle_request] (/home/vincent/WebstormProjects/nuxtExpress/node_modules/express/lib/router/layer.js:95:5)
   at trim_prefix (/home/vincent/WebstormProjects/nuxtExpress/node_modules/express/lib/router/index.js:312:13)
   at /home/vincent/WebstormProjects/nuxtExpress/node_modules/express/lib/router/index.js:280:7

The check in the render function in lib/nuxt.js fails because this is actually undefined.

// nuxt.js
render (req, res) {
    if (!this.renderer) { // <--- Here this is undefined
      setTimeout(() => {
        this.render(req, res)
      }, 1000)
      return
    }
    // ...

This may be caused by my code? I'm probably doing something wrong in this file.

// app.js (at project's root)
const express = require('express');
const app = express();
const Nuxt = require('nuxt');
const nuxtConfig = require('./config/nuxt.config');

new Nuxt(nuxtConfig).then((nuxt) => {
    app.use(nuxt.render);

    app.listen(3000, function () {
        console.log('listening port 3000 -> http://localhost:3000');
    });
}).catch((error) => {
    console.log(error);
});
// config/nuxt.config.js (from project's root)
module.exports = {
    build: {
        vendor: ['axios']
    }
};
<!-- pages/index.vue -->
<template>
    <div>
        <div>this is template body</div>
        <p class="ita">loaded from {{ req }}</p>
    </div>
</template>
<style>
    body{
        background-color: #f9f9f9;
        text-align: center;
    }

    p.ita {
        padding: 10px;
        font-style: italic;
    }
</style>
<script>
    export default{
        data({ req }){
            return{
                msg:'hello vue',
                req: req ? 'server' : 'client'
            }
        }
    }
</script>

Thanks, and keep up the good work! 👍

This bug report is available on Nuxt.js community (#c15)
@VincentLoy
Copy link
Author

VincentLoy commented Nov 28, 2016

I solved the problem by binding the render function 👍

const express = require('express');
const app = express();
const Nuxt = require('nuxt');
const nuxtConfig = require('./config/nuxt.config');

new Nuxt(nuxtConfig).then((nuxt) => {
    let render = nuxt.render;
    app.use(render.bind(nuxt));

    app.listen(3000, function () {
        console.log('listening port 3000 -> http://localhost:3000')
    });
}).catch((error) => {
    console.log(error);
});

@atinux atinux added the bug label Nov 28, 2016
@atinux
Copy link
Member

atinux commented Nov 28, 2016

Hi @VincentLoy , thank you for reporting this issue!

You don't have to bind the render function anymore with the last release v0.7.7

You should now be able to do:

app.use(nuxt.render)

@lock
Copy link

lock bot commented Nov 5, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Nov 5, 2018
@danielroe danielroe added the 2.x label Jan 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants