评论

这样写不能赋值吗?属实开了眼了😅

代码赋值问题

下面代码没有报错,我觉得没问题,检查了好几遍,服务器传过来有数据,同样代码放visual studio都正常,但prompt始终为空,微信开发者工具真是太库辣😅😅😅

var prompt: { id: number, promptname: string, prompt: string, valuesname: string,values: string,username: string,account: string }[]=[];

onLoad() {

      this.getprompt()

    },

getprompt(){

      var that = this

      wx.request({

        url: 'http://'+app_element.globalData.ip+':1337/getprompt',//访问路由

        method:'POST',

        data:{

          account:app_element.globalData.account,

          password:app_element.globalData.password

        },

        success: function (res) {

          var temp = JSON.stringify(res.data);

          var tempres = JSON.parse(temp);

          console.log(tempres)

          var tempprompt: { id: number, promptname: string, prompt: string, valuesname: string,values: string,username: string,account: string }[]=[]

              for(var m = 0; m < tempres.length; m++){

                  tempprompt.push ({  

                    id: tempres[m].id,  

                    promptname: tempres[m].promptname,  

                    prompt:tempres[m].prompt,  

                    valuesname: tempres[m].valuesname, 

                    values: tempres[m].values, 

                    username: tempres[m].username,

                    account: tempres[m].account

                  });

              }

              that.setData({

                prompt:tempprompt

              })

      }

    })

    },


最后一次编辑于  2023-12-13  
点赞 1
收藏
评论

3 个评论

  • 跨商通
    跨商通
    2023-12-14

    我在其他地方用prompt数组渲染时发现为空”,有这句话,判断是JS异步问题。去百度了解一下这个课题。

    2023-12-14
    赞同
    回复
  • Y.
    Y.
    2023-12-13

    这里为空吗


    2023-12-13
    赞同
    回复 3
    • 恶神桃花
      恶神桃花
      2023-12-13
      tempres打印出来有值,甚至tempprompt也有值,我在其他地方用prompt数组渲染时发现为空,问题可能出在这里
      that.setData({
                      prompt:tempprompt
                    })
      2023-12-13
      回复
    • Y.
      Y.
      2023-12-14回复恶神桃花
      2023-12-14
      1
      回复
    • Y.
      Y.
      2023-12-14回复恶神桃花
      不行就直接把var tempprompt: { id: number, promptname: string, prompt: string, valuesname: string,values: string,username: string,account: string }[]=[] 
      改成
      var tempprompt = []
      2023-12-14
      1
      回复
  • 恶神桃花
    恶神桃花
    2023-12-13

    这是服务器数据

    [

    {

        id: 0,

        promptname: '添加模板',

        prompt: '写一篇字数{number}的工作总结',

        valuesname: '字数',

        values: 'number',

        username: '山里灵活的狗',

        account: '1'

      },

    {

        id: 1,

        promptname: '学习报告',

        prompt: '写一篇主题为{theme}字数{number}的学习报告',

        valuesname: '主题,字数',

        values: 'theme,number',

        username: '山里灵活的狗',

        account: '1'

      },

    {

        id: 2,

        promptname: '测试',

        prompt: '啊啊{a}',

        valuesname: '啊',

        values: 'a',

        username: '恶神桃花',

        account: '2'

      },

    {

        id: 3,

        promptname: '3',

        prompt: '3',

        valuesname: '3',

        values: '3',

        username: '3',

        account: '3'

      },

    {

        id: 4,

        promptname: '4',

        prompt: '4',

        valuesname: '4',

        values: '4',

        username: '4',

        account: '4'

      }

    ]

    2023-12-13
    赞同
    回复 3
    • Y.
      Y.
      2023-12-13
      先打印res看  console.log(res)
      2023-12-13
      1
      回复
    • 恶神桃花
      恶神桃花
      2023-12-13回复Y.
      2023-12-13
      回复
    • 恶神桃花
      恶神桃花
      2023-12-13
      第三行是我设置了一个按键,按下后打印prompt的值,为空
      2023-12-13
      回复
登录 后发表内容