开发者社区 问答 正文

H5微应用: runtime.permission.requestAuthCode

import * as dd from 'dingtalk-jsapi';
// 此方式为整体加载,也可按需进行加载

dd.runtime.permission.requestAuthCode({
corpId: "ding8fb22",
onSuccess: function (info) {
console.log(info);
// 确保 info 中包含 'body' 属性
},
onFail: function (err) {
console.log(err);
}
});

报错errorMessage: "{\"errorCode\":\"-1\",\"errorMessage\":\"Cannot read properties of undefined (reading 'body')\"}"
如何解决?

展开
收起
游客lopzlmjf7arum 2024-01-21 19:07:58 250 分享 版权
1 条回答
写回答
取消 提交回答
  • 面对过去,不要迷离;面对未来,不必彷徨;活在今天,你只要把自己完全展示给别人看。

    这个错误是因为在onSuccess回调函数中,尝试访问info.body时,info对象可能是undefined。为了解决这个问题,你可以在访问info.body之前检查info是否为undefined

    解决方案如下:

    import * as dd from 'dingtalk-jsapi';
    
    dd.runtime.permission.requestAuthCode({
      corpId: "ding8fb22",
      onSuccess: function (info) {
        if (info) {
          console.log(info);
          // 确保 info 中包含 'body' 属性
        } else {
          console.log('info is undefined');
        }
      },
      onFail: function (err) {
        console.log(err);
      }
    });
    

    这样,当infoundefined时,会输出提示信息,而不是抛出错误。

    2024-01-21 19:41:04
    赞同 展开评论
问答分类: