小程序错误:Setting data field "collected" to undefined is invalid.

在实现新闻小程序的文章收藏功能时,第一次点击新闻详情时控制台正常输出,但是当我第再次点击查看其他文章详情的时候,控制台会抛出错误:Setting data field "collected" to undefined is invalid.

经过一下午的时间,终于解决了这个错误。

出现这个错误的原因是在页面加载时会从缓存里获取文章的收藏状态,只有点进文章详情时,才会将该文章的收藏状态存到缓存里。没有点进过的文章的收藏状态是为空的。在小程序里,数据如果为空,会将其转为undefined。所以系统会抛出这个错误。

解决方法:进行setDate操作之前对postCollected进行判断,判断其是否为空。如果为空则将collected设为false,收藏按钮将会显示灰色的图片。

var postsCollected = wx.getStorageSync("posts_collected");
    // ----问题所在位置----
    if(postsCollected){  //postsCollected为真的情况,在缓存中存在
        // debugger;
        var postCollected = postsCollected[postId];  // 读取其中一个缓存状态
        // this.setData.postsCollected = postsCollected;
        // 需要对获取到的状态做判断,如果没有点击过收藏按钮的文章,缓存中是没有这个数据的,所有会提示collected undefined
        if(!postCollected){
          this.setData({
            collected: false
          })
        }else{
          this.setData({
            collected: postCollected
          })
        }

 

 

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
We live in a world of big data: the amount of information collected on human behavior each day is staggering, and exponentially greater than at any time in the past. Additionally, powerful algorithms are capable of churning through seas of data to uncover patterns. Providing a simple and accessible introduction to data mining, Paul Attewell and David B. Monaghan discuss how data mining substantially differs from conventional statistical modeling familiar to most social scientists. The authors also empower social scientists to tap into these new resources and incorporate data mining methodologies in their analytical toolkits. Data Mining for the Social Sciences demystifies the process by describing the diverse set of techniques available, discussing the strengths and weaknesses of various approaches, and giving practical demonstrations of how to carry out analyses using tools in various statistical software packages. Table of Contents PART 1. CONCEPTS Chapter 1. What Is Data Mining? Chapter 2. Contrasts with the Conventional Statistical Approach Chapter 3. Some General Strategies Used in Data Mining Chapter 4. Important Stages in a Data Mining Project PART 2. WORKED EXAMPLES Chapter 5. Preparing Training and Test Datasets Chapter 6. Variable Selection Tools Chapter 7. Creating New Variables Using Binning and Trees Chapter 8. Extracting Variables Chapter 9. Classifiers Chapter 10. Classification Trees Chapter 11. Neural Networks Chapter 12. Clustering Chapter 13. Latent Class Analysis and Mixture Models Chapter 14. Association Rules

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值