首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >TypeError: path.startsWith不是函数

TypeError: path.startsWith不是函数
EN

Stack Overflow用户
提问于 2021-12-03 10:49:47
回答 1查看 182关注 0票数 0

我的函数:

代码语言:javascript
运行
复制
export const getStrapiUrl = (path): string => {
  if (path == null) {
    return "";
  }
  console.log(path);
  return `${
    path.startsWith("/", 1) ? process.env.NEXT_PUBLIC_STRAPI_API_URL : ""
  }${path}`;
};

来自控制台的错误消息:

{ url:'/uploads/work_with_74e76d4826.png‘}

代码语言:javascript
运行
复制
error - utils\get-strapi-url.ts (7:9) @ getStrapiUrl
TypeError: path.startsWith is not a function
   5 |   console.log(path);
   6 |   return `${
>  7 |     path.startsWith("/", 1) ? process.env.NEXT_PUBLIC_STRAPI_API_URL : ""
     |         ^
   8 |   }${path}`;
   9 | };
  10 | 
EN

回答 1

Stack Overflow用户

发布于 2021-12-03 10:58:15

根据输出{ url: '/uploads/work_with_74e76d4826.png' }path不是一个字符串,它是一个对象。但是,您可以提取其中的url部分:

代码语言:javascript
运行
复制
return `${
  path.url.startsWith("/", 1) ? process.env.NEXT_PUBLIC_STRAPI_API_URL : ""
}${path.url}`;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70213104

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档