上一下我自己的写法,可能有点绕了 const cloud = require('wx-server-sdk') cloud.init() exports.main = async (event, context) => { var request = require('request'); var that = this; async function wait(x) { return new Promise((resolve,reject)=>{ setTimeout(()=>{resolve(x)},3000) }) } var result = request({ url: 'xxx', data: {}, header: {}, method: 'POST', dataType: 'json', responseType: 'text', success: async (res) => { let result = await wait(res) return result}, fail: function(res) {}, complete: function(res) {}, }) return await result }
请问如何使云函数返回result?云函数:test.js(xxx是我的网站,不方便写出,已经测试过是可用的,可以返回数据,但是写入云函数后结果是null) [代码]// 云函数入口文件[代码][代码]const cloud = require([代码][代码]'wx-server-sdk'[代码][代码])[代码] [代码]cloud.init()[代码] [代码]exports.main = async (event, context) => {[代码][代码] [代码][代码]var[代码] [代码]request = require([代码][代码]'request'[代码][代码]);[代码][代码] [代码][代码]var[代码] [代码]that = [代码][代码]this[代码][代码]request ({[代码][代码] [代码][代码]url: [代码][代码]'xxx'[代码][代码],[代码][代码] [代码][代码]data: [代码][代码]''[代码][代码],[代码][代码] [代码][代码]header: {},[代码][代码] [代码][代码]method: [代码][代码]'GET'[代码][代码],[代码][代码] [代码][代码]dataType: [代码][代码]'json'[代码][代码],[代码][代码] [代码][代码]responseType: [代码][代码]'text'[代码][代码],[代码][代码] [代码][代码]success: res => {[代码][代码] [代码][代码]return[代码] [代码]res.data[代码][代码] [代码][代码]},[代码][代码] [代码][代码]fail: res => { },[代码][代码] [代码][代码]complete: res => { },[代码][代码] [代码][代码]})[代码][代码]}[代码] index.js [代码]testFunction() {[代码][代码] [代码][代码]wx.cloud.callFunction({[代码][代码] [代码][代码]name: [代码][代码]'test'[代码][代码],[代码][代码] [代码][代码]data: {[代码][代码] [代码][代码]},[代码][代码] [代码][代码]success: res => {[代码][代码] [代码][代码]wx.showToast({[代码][代码] [代码][代码]title: [代码][代码]'调用成功'[代码][代码],[代码][代码] [代码][代码]})[代码][代码] [代码][代码]console.log(res)[代码][代码] [代码][代码]},[代码][代码] [代码][代码]fail: err => {[代码][代码] [代码][代码]wx.showToast({[代码][代码] [代码][代码]icon: [代码][代码]'none'[代码][代码],[代码][代码] [代码][代码]title: [代码][代码]'调用失败'[代码][代码],[代码][代码] [代码][代码]})[代码][代码] [代码][代码]console.error([代码][代码]'[云函数] A 调用失败:'[代码][代码], err)[代码][代码] [代码][代码]}[代码][代码] [代码][代码]})[代码][代码] [代码][代码]}[代码] [图片] 本人新手,还很多东西不懂 请问各位高人,应该如何修改,才能得到result?
2019-09-07