You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

咨询:Moralis云函数未返回预期JSON字符串且日志无输入如何解决

Moralis云函数无返回问题修复方案

问题根因

  • 云函数定义参数错误:Moralis.Cloud.define 回调仅接收1个request参数,所有调用时传入的参数都存储在request.params对象中,你定义的第二个参数uri永远为undefined,所以无法获取输入内容
  • 混用await和.then逻辑冲突:await httpRequest.then(...) 得到的是.then回调的返回值,你没有在.then里返回任何内容,所以results最终为undefined
  • URL硬编码未使用传入参数:当前云函数里的请求URL是固定值,没有用到你调用时传入的uri参数
  • 多余的await:返回的results已经是字符串类型,不需要再次await

修复后代码

云函数定义

Moralis.Cloud.define("getMeta", async (request) => {
  const logger = Moralis.Cloud.getLogger();
  // 从请求参数中获取传入的uri
  const uri = request.params.uri;
  let results; 
  try {
    // 直接用await接收请求结果,不用额外写then
    const httpResponse = await Moralis.Cloud.httpRequest({
      url: uri, // 如果要固定请求463接口,可直接替换为对应固定地址
      followRedirects: true,
      headers: {
        'Content-Type': 'application/json;charset=utf-8'
      }
    });
    results = httpResponse.text;
    logger.info("over here"+results);
    logger.info(httpResponse.text);
  } catch (httpResponse) {
    logger.info('Request failed with response code ' + httpResponse.status);
    results = JSON.stringify({code: httpResponse.status, msg: '请求失败'});
  }
  logger.info("end"+results);
  return results;
});

调用代码

// 调用时参数要以对象形式传入,key和云函数里取params的key对应
let mData = await Moralis.Cloud.run("getMeta", {uri: '你要请求的目标地址'});

内容的提问来源于stack exchange,提问作者Yomoo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 22:51:01