代码与课程一致但是一直报错,找不出问题

来源:22-5 编写电影详情页面(下)

jjboy

2019-08-15

VM1421:1 根据 sitemap 的规则[0],当前页面 [pages/movies/movie-detail/movie-detail?id=305] 将被索引
console.warn @ VM1421:1
(anonymous) @ VM1472:6
(anonymous) @ VM1472:13
VM1421:1 thirdScriptError
Cannot read property ‘0’ of undefined;at pages/movies/movie-detail/movie-detail onLoad function;at api request success callback function
TypeError: Cannot read property ‘0’ of undefined
at r.processDoubanData (http://127.0.0.1:62457/appservice/pages/movies/movie-detail/movie-detail.js:42:29)
at Function.success (http://127.0.0.1:62457/appservice/utils/utils.js:24:7)
at Object.success (http://127.0.0.1:62457/appservice/dev/WAService.js:3:14159)
at c. (http://127.0.0.1:62457/appservice/dev/WAService.js:10:15121)
at c.emit (http://127.0.0.1:62457/appservice/dev/WAService.js:4:28151)
at Function. (http://127.0.0.1:62457/appservice/dev/WAService.js:10:16447)
at http://127.0.0.1:62457/appservice/dev/WAService.js:4:9966
at n (http://127.0.0.1:62457/appservice/dev/asdebug.js:1:3047)
at t. (http://127.0.0.1:62457/appservice/dev/asdebug.js:1:3535)
at t.emit (http://127.0.0.1:62457/appservice/dev/asdebug.js:1:7435)

在这里输入代码**utils.js:**
function convertToStarsArray(stars) {
  var num = stars.toString().substring(0, 1);
  var array = [];
  for (var i = 1; i <= 5; i++) {
    if (i <= num) {
      array.push(1);
    } else {
      array.push(0);
    }
  }
  return array;
}

function http(url, callBack) {
  wx.request({
    url: url,
    method: 'GET',
    header: {
      "Content-Type": "json"
    },
    success: function(res) {
      callBack(res.data);
    },
    fail: function(error) {
      console.log(error)
    }
  })
}

function convertToCastString(casts) {
  var castsjoin = "";
  for (var idx in casts) {
    castsjoin = castsjoin + casts[idx].name + " / ";
  }
  return castsjoin.substring(0, castsjoin.length - 2);
}

function convertToCastInfos(casts) {
  var castsArray = []
  for (var idx in casts) {
    var cast = {
      img: casts[idx].avatars ? casts[idx].avatars.large : "",
      name: casts[idx].name
    }
    castsArray.push(cast);
  }
  return castsArray;
}

module.exports = {
  convertToStarsArray: convertToStarsArray,
  http: http,
  convertToCastString: convertToCastString,
  convertToCastInfos: convertToCastInfos
**movie-detail.js**
var app = getApp();
var util = require("../../../utils/utils.js")
// pages/movies/movie-detail/movie-detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    movie: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var movieId = options.id;
    var url = app.globalData.doubanBase + "/v2/movie/subject/" + movieId;
    util.http(url, this.processDoubanData);
  },

  processDoubanData: function(data) {
    if(!data){
      return;
    }
    var director = {
      avatar: "",
      name: "",
      id: ""
    }
    if (data.directors[0] != null) {
      if (data.directors[0].avatars != null) {
        director.avatar = data.directors[0].avatars.large
      }
      director.name = data.directors[0].name;
      director.id = data.directors[0].id
    }
    var movie = {
      movieImg: data.images ? data.images.large : "",
      country: data.coutries[0],
      title: data.title,
      originalTitle: data.original_title,
      wishCount: data.wish_count,
      commentCount: data.commnents_count,
      year: data.year,
      generes: data.genres.join(","),
      stars: util.convertToStarsArray(data.rating.stars),
      score: data.rating.average,
      director: director,
      casts: util.convertToCastString(data.casts),
      castsInfo: util.convertToCastInfos(data.casts),
      summary: data.summary
    }
    console.log(movie)
    this.setData({
      movie: movie
    })
  }


})
写回答

3回答

嘉文十六世

2019-11-27

我也是报了跟你一样的错误,代码跟课程上老师讲的一样啊,请问你是怎么解决的

0
1
嘉文十六世
已解决,有一行directors[0]打成director[0]了
2019-11-27
共1条回复

入门学习的人儿

2019-08-20

请问你是怎么解决的,我跟你报一样的错误,感觉代码没有什么问题,调试后director{}可以获取到值,但是movie{}获取不到值。

0
2
入门学习的人儿
回复
jjboy
十分感谢,解决了
2019-08-21
共2条回复

jjboy

提问者

2019-08-15

已解决

0
0

微信小程序入门与实战(全新版) 超20000人学习的好课

4年同步微信官方迭代,累计20000+人学习, 比微信官方更火爆!

23866 学习 · 6899 问题

查看课程