收藏
回答

云函数发送HTTP请求失败

框架类型 问题类型 终端类型 AppID 环境ID 基础库版本
小程序 Bug 工具 wx4c041fb22aceb394 simple-0x5eh 2.8.1

https://cloud.tencent.com/edu/learning/course-1296-4324

我根据这个课程试验请求外部http

WAService.js:1 Uncaught (in promise) Error: errCode: -404011 cloud function execution error | errMsg: cloud.callFunction:fail requestID 158db8b8-1073-11eb-8057-525400c2bfee, cloud function service error code -504002, error message TypeError [ERR_INVALID_URL]: Invalid URL: httpbin.org/post
    at onParseError (:21211/appservice/internal/url.js:241)
    at new URL (:21211/appservice/internal/url.js:319)
    at Object.exports.default (:21211/var/user/node_modules/got/dist/source/core/utils/options-to-url.js:35)
    at normalizeArguments (:21211/var/user/node_modules/got/dist/source/core/index.js:480)
    at got (:21211/var/user/node_modules/got/dist/source/create.js:112)
    at Runtime.exports.main [as handler] (:21211/var/user/index.js:12)
    at Runtime.handleOnce (:21211/var/runtime/node10/Runtime.engine.js:214)
    at Timeout.setTimeout [as _onTimeout] (:21211/var/runtime/node10/Runtime.engine.js:54)
    at ontimeout (:21211/appservice/timers.js:436)
    at tryOnTimeout (:21211/appservice/timers.js:300); at cloud.callFunction api; 
    at new u (WAService.js:1)
    at d (WAService.js:1)
    at f (WAService.js:1)
    at Function.success (WAService.js:1)
    at WAService.js:1
    at C (WAService.js:1)
    at i. (WAService.js:1)
    at i.emit (WAService.js:1)
    at Rs (WAService.js:1)
    at WAService.js:1


一直反馈这个bug,我代码和步骤是跟着做的。


我在app.js里面定义了

      wx.cloud.init({
        // env 参数说明:
        //   env 参数决定接下来小程序发起的云开发调用(wx.cloud.xxx)会默认请求到哪个云环境的资源
        //   此处请填入环境 ID, 环境 ID 可打开云控制台查看
        //   如不填则使用默认环境(第一个创建的环境)
        env: "",//这里已经填了我的env
        traceUser: true
      })


云函数端httptest/index.js里面

// 云函数入口文件
const cloud = require('wx-server-sdk')


const got = require('got'); //引用 got


cloud.init()


// 云函数入口函数
exports.main = async(event, context) => {
  //let getResponse = await got('httpbin.org/get') //get请求 用httpbin.org这个网址做测试
  //return getResponse.body
  let postResponse = await got('httpbin.org/post', {
    method: 'POST', //post请求
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({ //把json数据(对象)解析成字符串
      title: "网址",
      value: 'anipc.com'
    })
  })


  return postResponse.body //返回数据
}


并且不管是云端安装依赖还是不安装依赖两种方式都试过了,并且在终端安装了got

https://my.oschina.net/u/4261674/blog/3708848

按照这个网址上写的一步步做了。但是还是一直报错,就是最上面的那个错误。求大神解答一下是什么问题?

云函数httptest文件夹里面的index.js

Page({
  http:function(e){
    wx.cloud.callFunction({ //调用云函数
      name:'httptest'           //云函数名为http
    }).then(res=>{      //Promise
      console.log(JSON.parse(res.result))
    })
  },


})

云函数httptest的package.json

{
  "name": "httptest",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "got": "^11.7.0",
    "wx-server-sdk": "~2.3.2"
  }
}


最后一次编辑于  2020-10-17
回答关注问题邀请回答
收藏

1 个回答

  • 哄哄
    哄哄
    2020-10-19

    常见的 Uncaught (in promise) Error,自主解决一下

    2020-10-19
    有用
    回复
登录 后发表内容
问题标签