Error: errCode: -401003 api parameter type error | errMsg: parameter.filePath should be string instead of object;
学习微信小程序云开发上传的时候遇到问题,期望解决,代码如下:
async submit(){
let that = this
let img = that.data.img
let img_detail = that.data.img_detail
wx.showLoading({
title: '上传中',
})
for(let i = 0 ; i<img.length;i++){
var timestamp=new Date().getTime()
await wx.cloud.uploadFile({
cloudPath: 'product/'+timestamp+''+i+''+'.png',
filePath: img[i], // 文件路径
}).then(async res => {
// get resource ID
console.log(res.fileID)
img[i] = res.fileID
if(i+1 == img.length){
console.log("bb")
for(let j = 0 ; j<img_detail.length;j++){
var timestamp_b=new Date().getTime()
await wx.cloud.uploadFile({
cloudPath: 'product_detail/'+timestamp_b+''+j+''+'.png',
filePath: img_detail[j], // 文件路径
}).then(res_1 => {
// get resource ID
console.log(res_1.fileID)
img_detail[j] = res_1.fileID
if(j+1 == img_detail.length){
wx.cloud.callFunction({
name:"product",
data:{
name:that.data.name,
h_price:that.data.h_price,
price:that.data.price,
img:img,
img_detail:img_detail,
select_classify:that.data.select_classify
}
}).then(pro=>{
wx.hideLoading()
console.log('',pro)
})
}
}).catch(error => {
// handle error
})
}
}
}).catch(error => {
console.log('',error)
// handle error
})
}
},
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({ env:"**************************"}) // 使用当前云环境
const db = wx.cloud.database()
// 云函数入口函数
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
return await db.collection('product').add({
data:{
time:db.serverDate(),
name:event.name,
h_price:event.h_price,
price:event.price,
img:event.img,
img_detail:event.img_detail,
select_classify:event.select_classify,
sale_volume:0
}
})
}
以下答案引用自GPT-3大模型,请合理使用:
,谢谢!
//代码更新:
async submit(){
let that = this
let img = that.data.img
let img_detail = that.data.img_detail
wx.showLoading({
title: '上传中',
})
for(let i = 0 ; i<img.length;i++){
var timestamp=new Date().getTime()
await wx.cloud.uploadFile({
cloudPath: 'product/'+timestamp+''+i+''+'.png',
filePath: img[i], // 文件路径
}).then(async res => {
// get resource ID
console.log(res.fileID)
img[i] = res.fileID
if(i+1 == img.length){
for(let j = 0 ; j<img_detail.length;j++){
var timestamp_b=new Date().getTime()
await wx.cloud.uploadFile({
cloudPath: 'product_detail/'+timestamp_b+''+j+''+'.png',
filePath: img_detail[j], // 文件路径
}).then(img_detail => {
// get resource ID
console.log(res_1.fileID)
img_detail[j] = res_1.fileID
if(j+1 == img_detail.length){
wx.cloud.callFunction({
name:"product",
data:{
name:that.data.name,
h_price:that.data.h_price,
price:that.data.price,
img:img,
img_detail:img_detail,
select_classify:that.data.select_classify
}
}).then(pro=>{
wx.hideLoading()
console.log('',pro)
}).catch(error => {
wx.hideLoading()
console.log('',error)
// handle error
})
}
}).catch(error => {
// handle error
})
}
}
}).catch(error => {
console.log('',error)
// handle error
})
}
},
如果我的回答解决了您的问题,请采纳我的回答
filePath应为字符串而不是对象,说明filePath 类型不对 ,你打印一下看看