收藏
回答

小程序莫名其妙的出现Setting data field “xxx“ to undefined ?

WXML代码如下
<van-tag type="danger" size="large" Position="center">请选取猪的身长(cm)</van-tag>
<input bindchange="getlength" class='input-radius' type="text" placeholder="请在此输入身长数据" placeholder-class="phcolor" />
<van-divider />
<van-tag type="danger" size="large" Position="center">请选取猪的体重(KG)</van-tag>
<input  bindchange="getweight" class='input-radius' type="text" placeholder="请在此输入体重数据" placeholder-class="phcolor" />
<van-divider />
<button bindtap="baogu" bindtap="dadou" bindtap="xiaomai" bindtap="dami" bindtap="xsd" bindtap="yan" >点击进行饲料配比(单位均为克)</button>
<text>苞谷(玉米)需要量:{{baoguweb}}</text>


<van-divider />
<text>小麦需要量:{{xiaomaiweb}}</text>
<van-divider />
<text>大米(稻米)需要量:{{damiweb}}</text>
<van-divider />
<text>小苏打(碳酸氢钠)需要量:{{xsdweb}}</text>
<van-divider />
<text>盐需要量:{{yanweb}}</text>
<van-divider />



JS代码如下:
var length,weight,baogu,dadou,xiaomai,dami,xsd,yan
Page({
  data: {
       baoguweb:"",
       dadouweb:"",
       xiaomaiweb:"",
       damiweb:"",
       xsdweb:"",
       yanweb:""
  },
getlength:function(event){
  length=event.detail.value
  console.log(length)
},
getweight:function(event){
  weight=event.detail.value
  console.log(weight)
},

baogu,dadou,xiaomai,dami,xsd,yan:function(event){
  if (length<70)  {
    baogu=Math.round(weight*1000*(0.5)*(0.2))
    dadou=Math.round(0)
    xiaomai=Math.round(weight*1000*(0.5)*(0.0225))
    dami=Math.round(weight*1000*(0.5)*(0.15))
    xsd=Math.round(weight*1000*(0.5)*(0.005))
    yan=Math.round(weight*1000*(0.5)*(0.0025))
    console.log(baogu)
    console.log(dadou)
    console.log(xiaomai)
    console.log(dami)
    console.log(xsd)
    console.log(yan)
    console.log(length)
  }
  else if (length>70&&length<120) {
    baogu=Math.round(weight*1000*(0.4)*(0.175))
    dadou=Math.round(0)
    xiaomai=Math.round(weight*1000*(0.4)*(0.0525))
    dami=Math.round(weight*1000*(0.4)*(0.125))
    xsd=Math.round(weight*1000*(0.4)*(0.005))
    yan=Math.round(weight*1000*(0.4)*(0.0025))
      console.log(baogu)
      console.log(dadou)
      console.log(xiaomai)
      console.log(dami)
      console.log(xsd)
      console.log(yan)
    }
  else if(length>120){
    baogu=Math.round(weight*1000*(0.35)*(0.15))
    dadou=Math.round(0)
    xiaomai=Math.round(weight*1000*(0.35)*(0.04))
    dami=Math.round(weight*1000*(0.35)*(0.10))
    xsd=Math.round(weight*1000*(0.35)*(0.005))
    yan=Math.round(weight*1000*(0.35)*(0.0025))
    console.log(baogu)
    console.log(dadou)
    console.log(xiaomai)
    console.log(dami)
    console.log(xsd)
    console.log(yan)
    console.log(length)
  }
  this.setData({
    baoguweb:baogu,
    dadouweb:dadou,
    xiaomaiweb:xiaomai,
    damiweb:dami,
    xsdweb:xsd,
    yanweb:yan,
    })
},
})
回答关注问题邀请回答
收藏

3 个回答

  • Demons
    Demons
    2022-05-30

    你试一下吧length填写值会不会报错

    2022-05-30
    有用
    回复 1
    • Kaiser
      Kaiser
      发表于移动端
      2022-05-30
      我把这里面的length换成数字没有报错
      2022-05-30
      回复
  • 朝酒晚舞
    朝酒晚舞
    2022-05-30

    你这个js代码骚的一p。。

    2022-05-30
    有用
    回复 5
    • Kaiser
      Kaiser
      2022-05-30
      做的毕设,刚开始还能正常运行,过了几天就报错了
      2022-05-30
      1
      回复
    • 朝酒晚舞
      朝酒晚舞
      2022-05-30回复Kaiser
      好想把你的代码拿过来改一波。
      2022-05-30
      回复
    • Kaiser
      Kaiser
      发表于移动端
      2022-05-30回复朝酒晚舞
      大佬 怎么联系您
      2022-05-30
      回复
    • Kaiser
      Kaiser
      发表于移动端
      2022-05-30回复朝酒晚舞
      刚答辩完,然后我这个小程序就用不了了
      2022-05-30
      回复
    • 朝酒晚舞
      朝酒晚舞
      2022-05-30回复Kaiser
      mcy661166
      2022-05-30
      回复
  • 王『小』贱*#
    王『小』贱*#
    2022-05-30

    我猜测,是因为你的赋值数据并没有定义,所以报了这么一堆,但是应该不会影响使用

    2022-05-30
    有用
    回复 8
    • Kaiser
      Kaiser
      发表于移动端
      2022-05-30
      我现在点按钮无法通过函数计算出来值了,提示Setting data field xxx to undefined
      2022-05-30
      回复
    • Kaiser
      Kaiser
      发表于移动端
      2022-05-30回复Kaiser
      我在第一行用var声明了这些变量
      2022-05-30
      1
      回复
    • 王『小』贱*#
      王『小』贱*#
      2022-05-30回复Kaiser
      我没看明白你的button为什么要bindtap一堆,然后又给那么多变量赋值成函数,在函数里再改成数值?你这个问题就是this.setdata的时候有undefined。你看一下你的调试器 AppData里面的值是不是你想要的
      2022-05-30
      回复
    • Kaiser
      Kaiser
      发表于移动端
      2022-05-30回复王『小』贱*#
      我做的这个小程序是输入身高体重得到需要的各饲料量 然后呢这个bindtap不是事件传参吗 我就想着都写进去了
      2022-05-30
      回复
    • 王『小』贱*#
      王『小』贱*#
      2022-05-30回复Kaiser
      你这传啥了啊,event在函数里都没用
      2022-05-30
      回复
    查看更多(3)
登录 后发表内容