小程序开发过程中发现一个我问题,我通过云平台在线测试代码是可以调用成功的,但是用云函数调用平台的测试代码就没有任何返回,也没有报错,请问是怎么回事?下面是云函数代码:
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());
});
}
因为最后的 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())
})
})
}