请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

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

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

3回答

嘉文十六世 2019-11-27 20:40:40

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

0 回复 有任何疑惑可以回复我~
  • 已解决,有一行directors[0]打成director[0]了
    回复 有任何疑惑可以回复我~ 2019-11-27 20:48:09
入门学习的人儿 2019-08-20 20:23:38

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

0 回复 有任何疑惑可以回复我~
  • 提问者 jjboy #1
    建议再核对一下代码,我检查以后发现我movie对象里面的变量名打错了,改正以后记好了
    回复 有任何疑惑可以回复我~ 2019-08-20 20:52:52
  • 入门学习的人儿 回复 提问者 jjboy #2
    十分感谢,解决了
    回复 有任何疑惑可以回复我~ 2019-08-21 09:09:09
提问者 jjboy 2019-08-15 21:10:22

已解决

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信