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.

Custom actions not receiving URL params

See original GitHub issue

Hi,

I can’t get custom actions on a resource to work correctly. I have the following resources.js:

import Vue from 'vue'

export var user = Vue.resource('/api/user', {}, {
  getCharacters: { method: 'GET', url: '/api/user/characters' }
})

export var character = Vue.resource(null, {}, {
  index: { method: 'GET', url: '/api/character' },
  search: { method: 'POST', url: '/api/character/search' },
  add: { method: 'POST', url: '/api/character' },
  verify: { method: 'POST', url: '/api/character{/id}/verify' },
  setMain: { method: 'POST', url: '/api/character{/id}/set-main' },
  remove: { method: 'DELETE', url: '/api/character{/id}' }
})

I’m importing it in a component and using it like this:

character.setMain({ id: this.character.id }).then(response => {
  ...
}, (response) => {
  ...
})

But this results in a request to /api/character/set-main, whereas it should be /api/character/559460/set-main. I’ve tried changing the URL structure to place the ID param at the end - same result. Also tried using the example given in the docs:

var customActions = {
  foo: {method: 'GET', url: 'someItem/foo{/id}'},
  setMain: {method: 'POST', url: 'someItem/bar{/id}'}
}

export var character = Vue.resource('someItem{/id}', {}, customActions)

Same result. This is on vue-resource 1.2.0.

Issue Analytics

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

github_iconTop GitHub Comments

5reactions
gabrielaraujofcommented, Oct 26, 2017

I found out what was happening @Riari. The actual call should be

character.setMain({ id: this.character.id }, {}).then(response => {
  ...
}, (response) => {
  ...
})

It is because if you only pass one parameter in, the resource will threat it like a body object for any method that uses the HTTP verbs POST, PUT, PATCH. Only if you pass two objects in, it will threat the first one as a params object.

I wonder if there is a better approach to this because it seems to me a little confusing, specially when the docs shows a wrong example.

1reaction
Noctsyndromecommented, Mar 20, 2017

Hi, I get the same issue for my current project. When using post/put method, the ‘id’ param become a request payload. I fix this by add ‘{}’ after the url param, like character.setMain( { id: id }, {} ).then()… try this out.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pass extra parameter to custom actions - Stack Overflow
From the DRF doc of Routing for extra actions. By default, the URL pattern is based on the method name, and the URL...
Read more >
Using custom actions with parameters in Amazon QuickSight
A custom action enables you to launch URLs or filter visuals by selecting a data point in a visual or choosing the action...
Read more >
Custom Actions for Links to Fields or URLs - Salesforce Help
Custom actions can use field parameters to link to a specific field on a record. Add your field parameters in the custom action...
Read more >
Custom Action for a URL Print Page - ServiceMax Help
In the Parameters section at the bottom, create one or more URL parameters. The parameter name cannot contain spaces.
Read more >
Create custom parameters for advanced tracking - Google Ads ...
Custom parameters are an advanced type of URL parameter that you can add to ... If you're not familiar with tracking in Google...
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