search

Home  >  Q&A  >  body text

Error when trying to use TypeScript instead of JavaScript: Expected 1-2 arguments but got 3.ts(2554)

<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粉420958692P粉420958692457 days ago546

reply all(1)I'll reply

  • P粉668113768

    P粉6681137682023-09-04 10:33:29

    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;
    }

    reply
    0
  • Cancelreply