当前位置:   article > 正文

NodeJS常见报错_the node.js path can contain

the node.js path can contain

Unexpected token T in JSON at position 0

报错原因

模板引擎书写中可能出现问题

解决方法

注意查看传递到模板中的变量是否存在、变量的数据类型等

Unexpected token R in JSON at position 0

报错原因

集合联合查询和渲染页面模板同时进行时会导致两者冲突,从而导致无法渲染页面

解决方法

包含两种不同情况

示例1

错误代码

const id = req.query.id;
const article = await Arcticle.findOne({ _id: id }).populate("author");
res.render('./home/article', {
    article: article
});
  • 1
  • 2
  • 3
  • 4
  • 5

解决方法
使用lean方法将集合联合的结果转化为普通对象

const article = await Arcticle.findOne({ _id: id }).populate("author").lean();
  • 1

示例2

错误代码
使用了第三方模块mongoose-sex-page实现分页功能

const pagination = require('mongoose-sex-page');

module.exports = async (req, res) => {
    const articles = await pagination(Arcticle).find({}).page(1).size(4).display(5).populate('author').exec();
    res.render('./home/default', {
        articles: articles
    });
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

解决方法

    const articles = await pagination(Arcticle).find({}).page(1).size(4).display(5).populate('author').exec();
    let string = JSON.stringify(articles);
    let obj = JSON.parse(string);
    res.render('./home/default', {
        articles: obj
    });
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

转自:
https://blog.csdn.net/weixin_50001396/article/details/112587981

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/449226
推荐阅读
相关标签
  

闽ICP备14008679号