Setting data field "collected" to undefined is invalid.

来源:19-7 使用缓存实现文章收藏功能

慕勒1183100

2018-07-18

var postsData = require('../../../data/posts-data.js')
Page({
data: {
},

onLoad: function(option) {
var postId = option.id;
var postData = postsData.postList[postId]; 
this.setData({
postData: postData,
postId: postId
})
var postsCollected = wx.getStorageSync('posts_collected')
if (postsCollected) {
var postCollected = postsCollected[postId]
this.setData({
collected: postCollected
})
} else { 
var postsCollected = {}; 
postsCollected[postId] = false;
wx.setStorageSync('posts_collected', postsCollected);
}
},

onColletionTap: function(event) {
var postsCollected = wx.getStorageSync('posts_collected');
var postCollected = postsCollected[this.setData.postId];
postCollected = !postCollected;
postsCollected[this.setData.postId] = postCollected; 
wx.setStorageSync('posts_collected', postsCollected); 
this.setData({
collected: postCollected
})
},
})
<view class='container'>
<image class='head-image' src='{{postData.img.headImgSrc}}'></image>
<image class='audio' src='../../../images/music-start.png'></image>
<view class='author-date'>
<image class='avatar' src='{{postData.img.avatarDetail}}'></image>
<text class='author'>{{postData.author}}</text>
<text class='const-text'>发表于</text>
<text class='date'>{{postData.dateTime}}</text>
</view>
<text class='title'>{{postData.title}}</text>
<view class='tool'>
<view class='circle-img'>
<image wx:if='{{collected}}' catchtap='onColletionTap' src='../../../images/shoucang.png'></image>
<image wx:else catchtap='onColletionTap' src='../../../images/shoucang-anti.png'></image>
<image catchtap='onShareTap' class='share-img' src='../../../images/fenxiang.png'></image>
</view>
<view class='horizon'></view>
</view>
<text class='detail'>{{postData.detail}}</text>
</view>

http://img.mukewang.com/szimg/5b4eb482000171e910170308.jpg

在点击某篇文章进入文章详情时就弹出了错误,“Setting data field "collected" to undefined is invalid.”

写回答

2回答

慕运维5448999

2018-09-16

同样问题,求解答

0
0

7七月

2018-07-19

错误给的很明显,你需要看看为什么出现undefined。

0
0

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

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

23867 学习 · 6899 问题

查看课程