收藏
回答

Cannot read property'routes' of undefined?

// pages/place/place.js


Page({


    /**
     * 页面的初始数据
     */
    data: {
        longitude: 115.8149,
        latitude: 32.8899,
        start:'',
        end:'',
        markers: [ {
            id: 0,
            latitude: 23.099994,
            longitude: 113.32452,
            width: 50,
            height: 50
        } ],
    result:[],
    routes:[]
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options{
        this.getPosition();
      },
    getPosition:function () {
        if(this.data.loading){
            return;
        }
        this.setData({loading:true});
        wx.getLocation({
            type:'wgs84',
            success:(res)=>{
                this.setData({
                    longitude:res.longitude,
                    latitude:res.latitude,
                    loading:false
                });
            },
            fail:(err)=>{
                console.log('Failed to get position',err);
                this.setData({
                    loading:false
                });
                wx.showToast({
                  title: '获取位置信息失败',
                  icon:'none'
                });
            }
        });
      },
      onSearch:function () {
        let start = this.data.start;
        let end = this.data.end;
        let that = this;
        wx.getLocation({
            type:'wgs84',
            success:(res)=>{
                wx.request({
                  url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + res.latitude + ',' + res.longitude + 
'&key=。。。。。。。。。。。。。。。。',
                  success:function (data{
                      let city=data.data.result.ad_info.city;
                      wx.request({
                        url: 'https://apis.map.qq.com/ws/direction/v1/transit/?from=' + start + '&to=' + end + '&city=' + city + 
'&key=。。。。。。。。。。。。。。。。。。。。。。。',
                        
                        success:res=>{
                            console.log('res:',res);
                            console.log('data:',res.data);
                            console.log(data.data.result);
                            let routes =res.data.result.routes;
                            that.setData({routes:routes});
                        },
                        fail:err=>{
                            console.log('err',err);
                            console.log(err);
                            that.setData({routes:[]});
                        }
                      });
                  },
                  fail:function (err{
                      console.log(err);
                  }
                });
            }
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {


    },


    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {


    },


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {


    },


    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {


    },


    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {


    },


    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {


    },


    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {


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

1 个回答

  • momo
    momo
    2023-05-11

    打印接口请求的结果,应该返回值有问题导致routes参数报错

    2023-05-11
    有用
    回复 3
    • 宴淮
      宴淮
      2023-05-11
      大佬,那应该怎么修改?
      2023-05-11
      回复
    • 宴淮
      宴淮
      2023-05-11
      看半天没看出头绪
      2023-05-11
      回复
    • momo
      momo
      2023-05-12回复宴淮
      截图看一下返回的res的值
      2023-05-12
      回复
登录 后发表内容