vue ssr express.js 报错:TypeError: Cannot read properties of null (reading 'records')

发布时间 2023-08-07 05:57:36作者: zjhgx

在vue ssr时,java后端返回的对象是内嵌对象的那种:

package com.davidhu.shopguide.api.bean.extend;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.davidhu.shopguide.api.bean.YunpanItem;
import com.davidhu.shopguide.api.bean.YunpanItemReply;

public class YunpanItemExtend {
    private YunpanItemBase item;

    private Page<YunpanItemReplyBase> firstReplyPage private Boolean invalid;

    private String contentStr;

    private Long redirectId;

    public Long getRedirectId() {
        return redirectId;
    }

   ...
}
public class Page<T> implements IPage<T> {

    private static final long serialVersionUID = 8545996863226528798L;

    /**
     * 查询数据列表
     */
    protected List<T> records = Collections.emptyList();

    /**
     * 总数
     */
    protected long total = 0;
    /**
     * 每页显示条数,默认 10
     */
    protected long size = 10;

    /**
     * 当前页
     */
    protected long current = 1;

如果返回的 firstReplyPage是空时,express会报错:

error occur
TypeError: Cannot read properties of null (reading 'records')
    at chunk-453.js:3215:59
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

所以只好改成了这样,初始化一个空对象确保不为空:

public class YunpanItemExtend {
    private YunpanItemBase item;

    private Page<YunpanItemReplyBase> firstReplyPage = new Page<YunpanItemReplyBase>();

    private Boolean invalid;

    private String contentStr;

    private Long redirectId;