Fetch : Argument of type 'string | undefined' is not assignable to parameter of type 'RequestInfo'

北城以北 提交于 2021-01-29 08:27:09

问题


Im receiving the following error in the "fetchUrl" argument in the fetch function

Argument of type 'string | undefined' is not assignable to parameter of type 'RequestInfo'.

In this function

let fetchUrl = getBaseUrlNative( process.env.APP_DATA, `/v2/marketing/app/file/${file}?cookies=${cookieSess}&expires=${Date.now()}`)

    await fetch(fetchUrl, { method: 'get' , cache:"no-store" , headers: {
        'Cache-Control': 'no-cache'
      }})
         .then(res => res.blob())
         .then(res => {  .......

getBaseUrlNative is

export const getBaseUrlNative = (eUrl : string | undefined, ext : string | undefined) => {
    try {
        if (!eUrl) throw new Error("Error .")
        return ext ? eUrl+ext : eUrl;
    } catch (err) {
        console.log(err)
    }
}

回答1:


Use the any data type in the fetch function to disallow type checking.

let fetchUrl = getBaseUrlNative( process.env.APP_DATA, `/v2/marketing/app/file/${file}?cookies=${cookieSess}&expires=${Date.now()}`)

    await fetch(<any>fetchUrl, { method: 'get' , cache:"no-store" , headers: {
        'Cache-Control': 'no-cache'
      }})
         .then(res => res.blob())
         .then(res => {  .......


来源:https://stackoverflow.com/questions/65832885/fetch-argument-of-type-string-undefined-is-not-assignable-to-parameter-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!