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.

TypeError: Cannot read property 'name' of undefined

See original GitHub issue

Description

Ziggy fails when you add parameters when they don’t exist.

Before version 1, I could just let the code be like this:

  axios.post(route(endpoint, this.food.id), data)
                    .then(this.saved)
                    .catch(this.failed);

The endpoint is dynamically set. However, if you have URLs that don’t have parameters it now fails.

VM12 letscook:115 Uncaught (in promise) TypeError: Cannot read property 'name' of undefined
    at VM12 letscook:115
    at Array.reduce (<anonymous>)
    at R.s (VM12 letscook:115)
    at new R (VM12 letscook:115)
    at VM12 letscook:115
    at _callee$ (offerFood.js:21)
    at tryCatch (runtime.js:45)
    at Generator.invoke [as _invoke] (runtime.js:274)
    at Generator.prototype.<computed> [as next] (runtime.js:97)
    at asyncGeneratorStep (CountryList.vue:45)

Expected behavior

It should work, even if the parameter is useless in some cases.

Current solution

// not that nice 😄

            let url = '';

            if (endpoint === 'uploadImage') {
                url = route(endpoint);
            } else {
                url = route(endpoint, this.food.id);
            }

            axios.post(url, data)
                .then(this.saved)
                .catch(this.failed);

Environment

  • Laravel version:  v8.14.0
  • Ziggy version:  v1.0.1

Related routes:

Route::post('/upload/image', 'Api\ImageUploadController@store')->name('uploadImage');

Ziggy.routes:

      "uploadImage": {
            "uri": "api\/upload\/image",
            "methods": ["POST"]
        },

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
bakerkretzmarcommented, Nov 23, 2020

@pmochine okay got it, thanks. I think I understand what’s happening, but I’m not sure that ignoring the extra param is actually the correct behaviour here. In an object, extra parameters are appended to the query string:

route(endpoint, { id: this.food.id });
// If the route has no params, this will return "http://endpoint.test/path?id=1"

Laravel’s route() helper also works this way, except it can handle strings and arrays too:

Route::get('no-params', function () {})->name('no-params');
Route::get('one-param/{param}', function () {})->name('one-param');

app('router')->getRoutes()->refreshNameLookups();

route('no-params');                 // "http://localhost/no-params"
route('no-params', 'foo');          // "http://localhost/no-params?foo"
route('one-param', 'foo');          // "http://localhost/one-param/foo"
route('one-param', ['foo', 'bar']); // "http://localhost/one-param/foo?bar"

I agree that Ziggy should not error here, but rather than ignoring the params, I would actually expect it to append them to the query like this. What do you think? I know this doesn’t solve your problem, sorry! 🙃

0reactions
bakerkretzmarcommented, Nov 26, 2020

Now that I suggested that I realize it’s not possible, since we’ve configured qs to filter out null query parameters… silently ignoring them might be the way to go after all! Still working on this, thanks for your patience.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Uncaught TypeError: Cannot read property 'name' of undefined
This type of error mean that your container variable file is not defined. You should use console.log at different places to see what...
Read more >
Uncaught TypeError: Cannot read property 'name' of undefined
It is a very common error when working with object and array to get a TypeError: Cannot read property 'name' of undefined ....
Read more >
TypeError: Cannot read property 'name' of undefined #20
~/projects/xxx/frontend npm run start ✓ 4s > talan-frontend@1.1.0 start > vue-cli-service serve INFO Starting development server.
Read more >
Cannot read property 'name' of undefined while using form ...
Getting TypeError: Cannot read property 'name' of undefined while using form-data ... This happens when some of the form value is undefined.
Read more >
Cannot read property 'name' of undefined' mean? - Quora
Usually it means you misspelled a variable name, or forgot to declare a variable, or it is out of scope.
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