Error when trying to use TypeScript instead of JavaScript: Expected 1-2 arguments but got 3.ts(2554)
P粉420958692
P粉420958692 2023-09-03 19:03:22
0
1
496
<p>When we use JavaScript, we have a Vue.js 2.6 application, but some code is written in TypeScript. I don't know much about TypeScript and trying to rewrite the code using Axios. It looks like this: </p> <p><strong>1) Caller: </strong></p> <pre class="brush:php;toolbar:false;">try { const params = { id: 1, inn: 2, withReferences: true, }; const result = await gpbApi.leadService.getPartnerReferences(params); } catch (error) { console.log('error = ', error); }</pre> <p><strong>2) Call: </strong></p> <pre class="brush:php;toolbar:false;">async getPartnerReferences(params: any) { if (!params) return; const { data } = await axios.get(`${path}/GroupAccountService/PartnerReferences`, params, { withCredentials: true }); return data.data; }</pre></p>
P粉420958692
P粉420958692

reply all(1)
P粉668113768

As Quentin pointed out in the comments, the axios documentation has one required parameter (url) and one optional parameter (config). Your code is passed three parameters, so the error is accurate, and the three-parameter get call is not doing what you'd expect in JS or TS.

However, the config parameter accepts a key named params, which is most likely the intended location of your params. You can use the Javascript abbreviation just use the name params instead of params: params. This means your fix is ​​simply to move the params to the inside (braces) of the object initializer.

If this code worked before, the params may have been in the object initializer on the same line as the URL, but were mistakenly moved outside the URL.

async getPartnerReferences(params: any) {
  if (!params) return;
  const { data } = await axios.get(`your.url`, {
    params, // this is now a part of the config object
    withCredentials: true
  });
  return data.data;
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!