微信小程序前端向后端输入图片出错
本地函数:
faceImage(){
console.log(111)
wx.chooseImage({
count:1,
sizeType:['compressed'],
sourceType:['album','camera'],
success:(res)=>{
console.log(res)
var tempFilePaths = res.tempFilePaths[0]
//解码base64//
var imgbase = wx.getFileSystemManager().readFileSync(res.tempFilePaths[0],'base64')
//console.log(imgbase)
//调用云函数
this.fruitcloud(imgbase)
this.setData({
botany:tempFilePaths
})
},
fail:(err)=>{
console.log(err)
}
})
},
//调用后端
fruitcloud(imgbase){
wx.cloud.callFunction({
name:'fruit',
data: {
img:imgbase
},
})
.then((res)=>{
console.log(res)
})
.catch((err)=>{
console.log(err)
})
}
})
后端云函数
const cloud = require('wx-server-sdk')
var AipImageClassifyClient = require("baidu-aip-sdk").imageClassify;
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境
// 设置APPID/AK/SK
var APP_ID = "2800933802";
var API_KEY = "zwZ2vLV265QXkdiG0OfafYHxh7n";
var SECRET_KEY = "Fc6adq8joPPFdppuSsdawd4QY2W11z5TX7iOQ";
// 新建一个对象,建议只保存一个对象调用服务接口
var client = new AipImageClassifyClient(APP_ID, API_KEY, SECRET_KEY);
var options = {};
options["top_num"] = "5";
// 云函数入口函数
exports.main = async (event, context) => {
let fruit = await aifruit()
console.log(fruit)
return{
fruit
}
//调用函数//
function aifruit(){
return new Promise((resolve,reject)=>{
client.ingredient(event.img, options)
.then((res)=>{
console.log(res)
})
.catch((err)=>{
console.log(err)
})
})
}
}
返回 res
"baidu-aip-sdk":"~2.4.6"
var AipImageClassifyClient = require("baidu-aip-sdk").imageClassify;
云函数的问题, 看起来是 语法错误 了 client.ingredient 不是函数
看一下代码第26行云函数的名称有没有写错,写好后有没有上传到云端
Uncaught (in promise) undefined
解题思路:
1、基础库版本调低
在微信开发者工具,右边详情里,把基础库版本调低
2、检查发现接口请求数据已经正常返回