HarmonyOS Type 'void' is not assignable to type 'string'. <ArkTSCheck>

在login.ets中使用

this.validCode = getAuthCode(this.phonenumber)

调用getAuthCode方法,报错Type ‘void’ is not assignable to type ‘string’. <ArkTSCheck>,请问如何解决呢》

getAuthCode方法如下,return在最后,中间部分不相关

return在最后,

return在最后,

return在最后,

import { getAuthCodeResponse, validCode } from '../../../bean/login';
import http from '@ohos.net.http';
import { LoginConstants } from '../../enum/loginEnum';

export function getAuthCode(phoneNum: string) {
  let httpRequest = http.createHttp();
  httpRequest.request(
    LoginConstants.getAuthCodeUrl,
    // 填写HTTP请求的URL地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定"EXAMPLE\_URL",
    {
      method: http.RequestMethod.POST, // 可选,默认为http.RequestMethod.GET
      // 开发者根据自身业务需要添加header字段
      header: {
        'Content-Type': 'application/json',
        'Authorization':'',
      },
      // 当使用POST请求时此字段用于传递内容
      extraData: {
        "data": {
          phoneNum:phoneNum,
        },
      },
      expectDataType: http.HttpDataType.STRING, // 可选,指定返回数据的类型
      usingCache: true, // 可选,默认为true
      priority: 1, // 可选,默认为1
      connectTimeout: 60000, // 可选,默认为60000ms
      readTimeout: 60000, // 可选,默认为60000ms
      usingProxy: false, //可选,默认不使用网络代理,自API 10开始支持该属性
    }).then(
    (data ) => {
      AlertDialog.show({message:JSON.stringify(data)})
      let result:validCode = data.result as validCode;
      // data为http.HttpResponse
      return result.authCode;
    })
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
HarmonyOS
2024-12-23 15:33:04
1.2w浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Excelsior_abit

代码中的return是在.then方法中return的,并不是在getAuthCode函数中return的,因此getAuthCode并没有返回值,所以会报Type ‘void’ is not assignable to type 'string’这个错,需要将result.authCode放在getAuthCode下return,才能获取到这个值

分享
微博
QQ
微信
回复
2024-12-23 18:58:39


相关问题
HarmonyOS type如何合并
504浏览 • 1回复 待解决
input_enter_key_type设置没生效
6631浏览 • 1回复 待解决
http请求中能否不设置Content-Type参数
2616浏览 • 1回复 待解决