收藏
回答

tencentcloud-sdk-nodejs.ImageModeration 调用成功没返回?

小程序开发过程中发现一个我问题,我通过云平台在线测试代码是可以调用成功的,但是用云函数调用平台的测试代码就没有任何返回,也没有报错,请问是怎么回事?下面是云函数代码:

const cloud = require('wx-server-sdk');

cloud.init();

const tencentcloud = require("tencentcloud-sdk-nodejs")



// 云函数入口函数

exports.main = async (event, context) => {

const TiiaClient = tencentcloud.tiia.v20190529.Client;

const models = tencentcloud.tiia.v20190529.Models;


const Credential = tencentcloud.common.Credential;

const ClientProfile = tencentcloud.common.ClientProfile;

const HttpProfile = tencentcloud.common.HttpProfile;


let cred = new Credential("AKIDzlJdkcMHFwNchWlVeXAZmmX3LSJvAgmd", "mKdgdKm0ziBwTlpcASUGqCJAl7F8lpvl");

let httpProfile = new HttpProfile();

httpProfile.endpoint = "tiia.tencentcloudapi.com";

let clientProfile = new ClientProfile();

clientProfile.httpProfile = httpProfile;

let client = new TiiaClient(cred, "ap-guangzhou", clientProfile);


let req = new models.ImageModerationRequest();


let params = '{"Scenes":["PORN"],"ImageUrl":"https://636c-cloudde-5c0a91-1300515261.tcb.qcloud.la/test.png"}'

req.from_json_string(params);


client.ImageModeration(req, function (errMsg, response) {

if (errMsg) {

console.log(errMsg);

return;

}

console.log(response.to_json_string());

});

}


回答关注问题邀请回答
收藏

1 个回答

  • uly
    uly
    2019-11-04

    因为最后的 ImageModeration 是 callback 风格的异步函数,需要改成 Promise 的,如下,只改了最后

    const cloud = require('wx-server-sdk')
     
    cloud.init()
     
    const tencentcloud = require('tencentcloud-sdk-nodejs')
     
    // 云函数入口函数
     

    exports.main = async (event, context) => {

         // ......

      return new Promise((resolve, reject) => {
        client.ImageModeration(req, function(errMsg, response) {
          if (errMsg) {
            console.log(errMsg)
     
            return reject(errMsg)
          }
     
          console.log(response.to_json_string())
          resolve(response.to_json_string())
        })
      })
    }


    2019-11-04
    有用 1
    回复 2
    • 2019-11-04
      非常感谢,按照你的方法已经解决了
      2019-11-04
      回复
    • 毋云好
      毋云好
      2020-05-16
      我使用ASR也这样,没有报错,返回resultID,但没有返回识别结果,如上尝试也一样,好痛苦呀
      2020-05-16
      回复
登录 后发表内容
问题标签