djf1(imgphat,phatvar) {
var that = this;
console.log(imgphat)
for (var i = 0; i < imgphat.length; i++) {
console.log(i);
wx.uploadFile({
url: 'https://www.xxx.top:8082/imgup.php',
filePath: imgphat[i],
name: 'file',
success: function (res) {
var a = JSON.parse(res.data);
if(phatvar=='PhotoName'){
that.setData({
PhotoName:that.data.PhotoName.concat('https://www.xxx.top:8082/'+a.data)
})
}
if(phatvar=='PhotoName1'){
that.setData({
PhotoName1:that.data.PhotoName1.concat('https://www.xxx.top:8082/'+a.data)
})
}
if (res) {
wx.showToast({
title: '已提交发布!',
duration: 3000
});
}
}
})
}
},
handleSubmit(){
var that = this;
var fromex = that.data.value3
if(fromex[0] =="" ){
this.djf2("名称不能为空");
}
if(fromex[1] ==""){
this.djf2("品牌不能为空");
}
if(fromex[2]=="" ){
this.djf2("型号不能为空");
}
if(fromex[3]== "" ){
this.djf2("类型不能为空");
}
if(fromex[4] == "" ){
this.djf2("数量不能为空");
}
if(fromex[5] == "" ){
this.djf2("单位不能为空");
}
if(fromex[6] == "" ){
this.djf2("区域不能为空");
}
if(fromex[7] == "" ){
this.djf2("时间不能为空");
}
if(fromex[8] =="" ){
this.djf2("维保名不能为空");
}
if(that.data.value4.length != 0 ){
that.djf1(that.data.value4,'PhotoName');
}
if( that.data.tempImageUrl.length != 0){
that.djf1(that.data.tempImageUrl,'PhotoName1');
}
console.log("一下等于多少")
console.log(this.data.PhotoName1.length)
wx.request({
url: 'https://www.xxx.top:8082/fromdata.php',
method:'POST',
data: {
'a':fromex,
'b':this.data.PhotoName,
'c':this.data.PhotoName1,
},
header: {
'content-type': 'application/x-www-form-urlencoded'
},
success (res) {
console.log(res)
}
})
},
该函数是异步执行的
wx.uploadFile函数的异步特性导致的,该函数是异步执行的,而且在djf1函数内部的循环启动了多个异步文件上传操作。由于wx.request函数没有等待所有上传完成就执行,可能导致PhotoName在第一次点击提交按钮时尚未完全赋值。你可以使用Promise或回调机制来确保在所有上传完成后再调用wx.request
是这样子写在函数吗,但是会报错
return new Promise(resolve, reject) => {//异步处理代码
}
[自动热重载错误 appservice 生成错误] pages/index/index.js: file: pages/index/index.js
unknown: Missing semicolon. (93:41)
91 | },
92 | djf1(imgphat,phatvar) {
> 93 | return new Promise(resolve, reject) => {}
| ^
94 | var that = this;
95 | console.log(imgphat)
96 | for (var i = 0; i < imgphat.length; i++) {(env: Windows,mp,1.06.2308310; lib: 2.19.4)
或者那位大佬可以给点思维我处理一下