SyntaxError: Unexpected token R in JSON at position 0

Error message

Error message

Error code

    // 查询所有文件数据  (多集合联合查询)
let articles = await Article.find().populate('author');
    // 渲染文章列表页面模板
    res.render('admin/article.art', {
    
    
        articles: articles
    });

Reason for error

  • When the collection joint query and the rendering of the page template are performed at the same time, the two conflicts, and the page cannot be rendered. So an error

Solution

The lean() method is used to transform the result of multi-level union into ordinary objects to alleviate the conflict between the two.

 let articles = await Article.find().populate('author').lean();

Another error code

let articles = await pagination(Article).find().page(1).size(1).display(3).populate('author').exec();
res.render('admin/article.art', {
    
    
        articles: articles
    });

Reason for error

When the collection joint query and the rendering of the page template are performed at the same time, the two conflicts, and the page cannot be rendered. So an error

Solved code

    let articles = await pagination(Article).find().page(1).size(1).display(3).populate('author').exec();
    let str = JSON.stringify(articles);
    let json = JSON.parse(str);
    // res.send(articles)
    // 渲染文章列表页面模板
    res.render('admin/article.art', {
    
    
        articles: json
    });
}

Transfer from: https://blog.csdn.net/qq_49002903/article/details/112541719

Self-motivation

Work harder and harder, and then wind and water will rise, all beings are suffering, and you can't give up.

Guess you like

Origin blog.csdn.net/weixin_50001396/article/details/112587981