javascript - Fetch: El argumento de tipo 'string | undefined' no se puede asignar al parámetro de tipo 'RequestInfo'
Estoy recibiendo el siguiente error en el argumento "fetchUrl" en la función fetch
El argumento de tipo 'string | undefined' no se puede asignar al parámetro de tipo 'RequestInfo'.
En esta función
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 es
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)
}
}
Respuesta #1
Es porque tu variable de entorno viene como indefinida antes de que la uses.
Usa la afirmación de tipo con la palabra clave:
como
Entonces algo como:
getBaseUrlNative( process.env.APP_DATA as string, ...)
Esto debería funcionar perfectamente.
editar/actualizar:
getBaseUrlNative( process.env.APP_DATA!, ...)
Esto también debería funcionar. Al incluir !, estamos diciendo que sabemos que el valor no será indefinido para cuando se renderice la página, por lo que podemos ignorar la advertencia.
Respuesta #2
Usa el tipo de dato any en la función fetch para no permitir la verificación de tipos.
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 => { .......