Setting data field "collected" to undefined is invalid.

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

慕函数1327521

2020-06-17

collection.pngcollection-anti.png var postsData = require('../../../data/posts-data.js') Page({ //借助data给下面的onCollectionTap 传递id data:{
  },

onLoad:function(option){
//接收posts.js 传递过来的postId
//posts.js中 url: “post-detail/post-detail?id=” + postId
var postId = option.id;
this.data.currentPostId = postId;
var postData = postsData.postList[postId];
//数据绑定
// this.data.postData = postData;
this.setData({
postData:postData
})
var postsCollected = wx.getStorageSync(“posts_collected”)
if(postsCollected){
var postCollected = postsCollected[postId]
//判断是否读取到,读取到的话就做数据绑定
if(postCollected){
this.setData({
collected: postCollected
})
}
}else{
var postsCollected = {};
postsCollected[postId] = false;
//设置缓存
wx.setStorageSync(‘posts_collected’, postsCollected)
}
},

  onCollectionTap:function(event){
    
    var postsCollected = wx.getStorageSync("posts_collected")
   var postCollected = postsCollected[this.data.currentPostId];
    
    postCollected: !postCollected;
   
    postsCollected[this.data.currentPostId] = postCollected;
    //更新文章是否被收藏的缓存值
    wx.setStorageSync("posts_collected", postsCollected);
    //更新数据绑定变量,从而实现切换图片 
    this.setData({
          collected: postCollected
    })
}

})

写回答

1回答

7七月

2020-06-17

调试一下。。。,这个只是纯粹的代码写错了。无论是再看课程,或者调试,或者对比源代码,总能找到哪里写错了

0
0

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

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

23867 学习 · 6899 问题

查看课程