上传图片的方法,上传一次调用一次接口,但是传了两张照片之后最后的数组里面变成了三项,其中两项是重复的。
接口返回正常
let formData = new FormData()
let newArray = []
this.productForm.productPicList.forEach((item, index) => {
this.file.forEach((item2, index2) => {
if (item.uid == item2.uid) {
newArray.push(item2)
}
})
})
newArray.forEach(item => {
formData.append('url', item)
})
console.log(newArray, 'newArraynewArray')
if (this.typeStatus == '新增') {
uploadImg(formData, 'product').then(response => {
response.data.forEach((item2, index) => {
console.log(item2, 'item00000000')
this.uploadApiArr.push({
attachment: item2.url,
number: this.productForm.productPicList[index].number,
id: '',
name: item2.fileName
})
console.log(this.uploadApiArr, ' this.uploadApiArr')
})
})
} else if (this.typeStatus == '编辑') {
uploadImg(formData, 'product').then(response => {
response.data.forEach(item => {
this.uploadApiArr.push({
attachment: item.url,
number: this.productForm.productPicList[index].number,
id: this.productForm.productPicList[index].id
})
})
})
}
最后打印出来是三项,传了两张照片
前两项都是重复,应该怎么解决,感谢各位
你和后端商量好了吗?没听说过上传一张图片后端返回图片数组的,那你怎么知道这数组里面哪一条对应的是你刚才提交的?这通常是两个接口,一个是上传处理,后端仅返回针对本次提交的结果,另一个接口是查询图片,返回图片数组。
如果你们实在懒得改,那你上传之后只检查是不是返回了200就好了,上传最后一张完成的时候再处理后端返回的数组。