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

TypeError: Cannot read property 'path' of undefined #41

Closed
antcook opened this issue Dec 6, 2016 · 10 comments
Closed

TypeError: Cannot read property 'path' of undefined #41

antcook opened this issue Dec 6, 2016 · 10 comments

Comments

@antcook
Copy link

antcook commented Dec 6, 2016

When I try to run the provided custom-routes example, I get the following error. I've also been able to replicate the bug in my own project by trying to use the hidden pages feature when defining routes.

TypeError: Cannot read property 'path' of undefined
    at VueRouter.match (C:\Users\Anthony\Desktop\custom-routes\node_modules\vue-router\dist\vue-router.common.js:1135:44)
    at VueRouter.resolve (C:\Users\Anthony\Desktop\custom-routes\node_modules\vue-router\dist\vue-router.common.js:2037:23)
    at Proxy.render (C:\Users\Anthony\Desktop\custom-routes\node_modules\vue-router\dist\vue-router.common.js:300:22)
    at VueComponent.Vue._render (C:\Users\Anthony\Desktop\custom-routes\node_modules\vue\dist\vue.runtime.common.js:2925:22)
    at renderComponent (C:\Users\Anthony\Desktop\custom-routes\node_modules\vue-server-renderer\build.js:5888:25)
    at renderNode (C:\Users\Anthony\Desktop\custom-routes\node_modules\vue-server-renderer\build.js:5871:7)
    at next (C:\Users\Anthony\Desktop\custom-routes\node_modules\vue-server-renderer\build.js:6007:9)
    at cachedWrite (C:\Users\Anthony\Desktop\custom-routes\node_modules\vue-server-renderer\build.js:36:9)
    at renderElement (C:\Users\Anthony\Desktop\custom-routes\node_modules\vue-server-renderer\build.js:5931:5)
    at renderNode (C:\Users\Anthony\Desktop\custom-routes\node_modules\vue-server-renderer\build.js:5875:7) 

I'm running Node version v6.9.1 on Windows 10.

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

antcook commented Dec 6, 2016

Update:

I have managed to fix this by providing the route with a name key, you may wan't to update the custom-routes example.

@alexchopin
Copy link
Member

Hi, thanks for your feedback.
The example is fixed now.

@antcook
Copy link
Author

antcook commented Dec 8, 2016

@alexchopin I'm getting this error again when using the auth-routes demo combined the hidden pages feature, I've replicated it in my own project. It seems that running node server.js throws a path error in vue-router, but running the nuxt command has no errors.

I've tried running nuxt build before starting server.js to see if that fixed it but it doesn't. Here is an example:

nuxt.config.js

module.exports = {
  router: {
    routes: [
      {name: 'demo', path: '/demo/:foo', component: 'pages/_demo'}
    ]
  }
}

Example router-link

<template>

  <div>
    
    <router-link :to="{name: 'demo', params: {foo: 'bar'}}">
      Demo Link
    </router-link>

  </div>

</template>

package.json

{
  "name": "auth-routes",
  "description": "",
  "dependencies": {
    "body-parser": "^1.15.2",
    "express": "^4.14.0",
    "express-session": "^1.14.2",
    "nuxt": "latest",
    "whatwg-fetch": "^2.0.1"
  },
  "scripts": {
    "dev": "node server.js",
    "build": "nuxt build",
    "start": "NODE_ENV=production node server.js"
  }
}

Running the following throws the path error

npm run dev
npm start

Running this throws NO error

npm build

Here is the exact error

Vue.js error

TypeError: Cannot read property 'path' of undefined
    at VueRouter.match (C:\Users\Anthony\Desktop\auth-routes\node_modules\vue-router\dist\vue-router.common.js:1135:44)
    at VueRouter.resolve (C:\Users\Anthony\Desktop\auth-routes\node_modules\vue-router\dist\vue-router.common.js:2037:23)
    at Proxy.render (C:\Users\Anthony\Desktop\auth-routes\node_modules\vue-router\dist\vue-router.common.js:300:22)
    at VueComponent.Vue._render (C:\Users\Anthony\Desktop\auth-routes\node_modules\vue\dist\vue.runtime.common.js:2925:22)
    at renderComponent (C:\Users\Anthony\Desktop\auth-routes\node_modules\vue-server-renderer\build.js:5888:25)
    at renderNode (C:\Users\Anthony\Desktop\auth-routes\node_modules\vue-server-renderer\build.js:5871:7)
    at next (C:\Users\Anthony\Desktop\auth-routes\node_modules\vue-server-renderer\build.js:6007:9)
    at cachedWrite (C:\Users\Anthony\Desktop\auth-routes\node_modules\vue-server-renderer\build.js:36:9)
    at renderElement (C:\Users\Anthony\Desktop\auth-routes\node_modules\vue-server-renderer\build.js:5931:5)
    at renderNode (C:\Users\Anthony\Desktop\auth-routes\node_modules\vue-server-renderer\build.js:5875:7)

I've tested on both Windows and Mac, both running Node 7.0.1, both has the same error

@Atinux
Copy link
Member

Atinux commented Dec 8, 2016

Hi @antcook

Can you show us your server.js file when you're requiring Nuxt and instantiating it please?

@antcook
Copy link
Author

antcook commented Dec 8, 2016

Using the server.js from the custom-routes example throws the error, and I have also tried starting a fresh project with the following server.js but still throws the same error:

const Nuxt = require('nuxt')
const bodyParser = require('body-parser')
const session = require('express-session')
const app = require('express')()

// Body parser, to access req.body
app.use(bodyParser.json())

// Sessions to create req.session
app.use(session({
  secret: 'super-secret-key',
  resave: false,
  saveUninitialized: false,
  cookie: { maxAge: 60000 }
}))

// We instantiate Nuxt.js with the options
const isProd = process.env.NODE_ENV === 'production'
const nuxt = new Nuxt({ dev: !isProd })
// No build in production
const promise = (isProd ? Promise.resolve() : nuxt.build())
promise.then(() => {
  app.use(nuxt.render)
  app.listen(3000)
  console.log('Server is listening on http://localhost:3000')
})
.catch((error) => {
  console.error(error)
  process.exit(1)
})

@Atinux
Copy link
Member

Atinux commented Dec 8, 2016

Can you check in the node_modules/nuxt/package.json which version is it?

Sorry about this

@antcook
Copy link
Author

antcook commented Dec 8, 2016

No problem, the version is 0.8.3

@alexchopin
Copy link
Member

alexchopin commented Dec 8, 2016

Hi,
It's because you need to import your nuxt.config.js before instantiate Nuxt in your server.js.
We still need to complete the documentation, sorry about that.
You can see this example :

const Nuxt = require('nuxt')
const bodyParser = require('body-parser')
const session = require('express-session')
const app = require('express')()

// Body parser, to access req.body
app.use(bodyParser.json())

// Sessions to create req.session
app.use(session({
  secret: 'super-secret-key',
  resave: false,
  saveUninitialized: false,
  cookie: { maxAge: 60000 }
}))

// POST /api/login to log in the user and add him to the req.session.authUser
app.post('/api/login', function (req, res) {
  if (req.body.username === 'demo' && req.body.password === 'demo') {
    req.session.authUser = { username: 'demo' }
    return res.json({ username: 'demo' })
  }
  res.status(401).json({ error: 'Bad credentials' })
})

// POST /api/logout to log out the user and remove it from the req.session
app.post('/api/logout', function (req, res) {
  delete req.session.authUser
  res.json({ ok: true })
})

// We instantiate Nuxt.js with the options
const isProd = process.env.NODE_ENV === 'production'
let config = require('./nuxt.config.js')
config.dev = !isProd
const nuxt = new Nuxt(config)
// No build in production
const promise = (isProd ? Promise.resolve() : nuxt.build())
promise.then(() => {
  app.use(nuxt.render)
  app.listen(3000)
  console.log('Server is listening on http://localhost:3000')
})
.catch((error) => {
  console.error(error)
  process.exit(1)
})

@antcook
Copy link
Author

antcook commented Dec 8, 2016

Thank you so much for your time! That did the trick :)

@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.
Projects
None yet
Development

No branches or pull requests

4 participants