收藏
回答

wx.saveFile(Object object) 为什么在我修改对象之前输出值就已经修改好了?

我使用的是Taro框架

// 保存本地数据列表
  const save_local_data_list = async e =>{
      let list = [ ...localDataList ]
      console.log(list)
      
      for (let d of list){
          await Taro.saveFile({
              tempFilePath: d.file,
              success: res =>{
                  console.log(res.savedFilePath)
                  d.filePath = res.savedFilePath
                  
              }
          })
      }
      console.log(list)
  }


我期待的输出是—— 第一个输出结果应该不包含 filePath这个变量,第二个输出中包含了filePath这个变量。

但是实际上—— 这两个结果都输出了filePath,这让我很懵呀,求助!!! 头疼的不行了。

我保证这个方法只调用了1次,因为在控制台中只有1遍输出,也就是说 save_local_data_list这个方法只调用了一次。



回答关注问题邀请回答
收藏

1 个回答

  • Pluto.
    Pluto.
    2021-07-01
    import {View} from '@tarojs/components'
    import Taro, { getCurrentInstance } from '@tarojs/taro'
    import { AtDivider, AtButton } from 'taro-ui'
    import Title from '@/components/TitleHandleData'
    import {local_data_path} from '@/config'
    
    
    import Item from './components/Item'
    import './index.less'
    import { useState } from 'react'
      
    
    
    const Index = p => {
        const objs = [
            {
                name: '长度',
                eglish_name: 'cd',
                type: 'text',
                tips: '请输入长度'
            }, {
                name: '质量',
                eglish_name: 'zl',
                type: 'text',
                tips: '请输入质量信息'
            }, {
                name: '材质',
                eglish_name: 'cz',
                type: 'text',
                tips: '请输入刀具材质'
            }, {
                name: '划痕',
                eglish_name: 'hh',
                type: 'text',
                tips: '请确认该刀具是否有划痕'
            }, {
                name: '凹坑',
                eglish_name: 'ak',
                type: 'text',
                tips: '请确认该刀具是否有凹坑'
            }, {
                name: '涂层剥落',
                eglish_name: 'tcbl',
                type: 'text',
                tips: '请确认该刀具是否有涂层剥落'
            }, {
                name: '边缘豁口',
                eglish_name: 'byhk',
                type: 'text',
                tips: '请确认该刀具是否有边缘豁口'
            }
        ]
        let {files} = getCurrentInstance().router.params
        files = JSON.parse(files)
    
    
        const [localDataList, setLocalDataList] = useState([])
        const handleLocalDataChange = ({index, data}) => {
            let ldl = localDataList
            ldl[index] = data
            setLocalDataList(ldl)
        }
    
    
        // 保存本地数据列表
        const save_local_data_list = async e =>{
            let list = [ ...localDataList ]
            console.log(list)
            
            for (let d of list){
                await Taro.saveFile({
                    tempFilePath: d.file,
                    success: res =>{
                        console.log(res.savedFilePath)
                        d.filePath = res.savedFilePath
                        
                    }
                })
            }
            console.log(list)
        }
    
    
        return (
            <View className='addItem'>
                <Title title='基于机器学习的刀具表面缺陷检测及分类方法' subtitle='补充数据'/>
                {files.map((f, i)=><Item key={f} index={i} img={f} objs={objs} handleLocalDataChange={handleLocalDataChange}/>)}
                <AtButton type='primary' className='btn' onClick={save_local_data_list}>加入任务</AtButton>
                <AtDivider content='没有更多了' />
            </View>
        ) 
    }
    export default Index
    
    2021-07-01
    有用
    回复 2
    • Pluto.
      Pluto.
      2021-07-01
      这里是全部的代码
      2021-07-01
      回复
    • Pluto.
      Pluto.
      2021-07-01
      如果有描述不清楚的,请您指出,我描述清晰一点
      2021-07-01
      回复
登录 后发表内容