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
})
}
})
4年同步微信官方迭代,累计20000+人学习, 比微信官方更火爆!
了解课程