简体   繁体   English

属性在类型字符串错误上不存在

[英]Property does not exist on type String error

I'm trying to send a bug report via API, and have an authentication token in my apps metadata settings, which works usually.我正在尝试通过 API 发送错误报告,并在我的应用程序元数据设置中有一个身份验证令牌,这通常有效。 I refactored that logic out of my main.js and into it's own class, but now I'm receiving 403 errors, which I can only imagine is due to how I'm referencing the token.我将该逻辑从我的 main.js 中重构到它自己的类中,但现在我收到了 403 错误,我只能想象这是由于我如何引用令牌。

  class Logger {

   constructor(settings) {
   // getting the settings here and assigning it to the constructor variable
     this.settings = settings;
   }
   static async logInfo(data = {}) {
     console.log('Hello!')
     const url = 'https://sampleUrl'
       const response = await fetch(url, {
       method: 'POST',
       headers: {
         "X-TrackerToken": `${metadata.settings.pivotal_token}`,
         "Content-Type": "application/json"
       },
       body: JSON.stringify(data)
     });
     return response.json();
   }
 }

 const metadata = {
   settings: ''
 }
 const logger = new Logger(metadata.settings);

How I call the Logger class in Main.js我如何在 Main.js 中调用 Logger 类

Logger.logInfo(metadata.settings, bugInfo).then(console.log('print'))
      })

On mouse hover the tooltip returns the message 'Property does not exist on type String error.', before the move into it's own class I referenced it with the same syntax, so not sure how to proceed here.在鼠标悬停时,工具提示会返回消息“字符串类型错误时不存在属性。”,在进入它自己的类之前,我使用相同的语法引用了它,因此不确定如何在此处进行。

You are passing the settings with the constructor, so you need to use this setting in the Logger class, but you are using another variable for X-TrackerToken that is not defined.您正在使用构造函数传递settings ,因此您需要在Logger类中使用此setting ,但您正在为X-TrackerToken使用另一个X-TrackerToken变量。

const metaData = {
  settings: ""
}

metadata.settings.pivotal_token // ---> undefined

You are trying to get pivotal_token from the settings which is a string and obviously, there is no pivotal_token method existing for your settings strings.您正在尝试从字符串设置中获取pivotal_token ,显然,您的settings字符串不存在pivotal_token方法。

You are doing metadta.settings.pivotal_token but metadta.settings is a string.您正在执行 metadta.settings.pivotal_token 但 metadta.settings 是一个字符串。 It's not having any pivotal_token property.它没有任何 pivotal_token 属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM