Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

express框架中调用render()出现栈溢出问题 #624

Open
pikzhang opened this issue Sep 9, 2020 · 4 comments
Open

express框架中调用render()出现栈溢出问题 #624

pikzhang opened this issue Sep 9, 2020 · 4 comments

Comments

@pikzhang
Copy link

pikzhang commented Sep 9, 2020

//控制台报错信息
RangeError: Maximum call stack size exceeded
at Buffer.get [Symbol.toStringTag] ()
at Buffer.toString ()
at toType (C:\Users\piCzhang\Desktop\project_blog\node_modules\art-template\lib\compile\adapter\extend.js:7:47)
at extend (C:\Users\piCzhang\Desktop\project_blog\node_modules\art-template\lib\compile\adapter\extend.js:20:16)
at extend (C:\Users\piCzhang\Desktop\project_blog\node_modules\art-template\lib\compile\adapter\extend.js:31:33)
at extend (C:\Users\piCzhang\Desktop\project_blog\node_modules\art-template\lib\compile\adapter\extend.js:31:33)
at extend (C:\Users\piCzhang\Desktop\project_blog\node_modules\art-template\lib\compile\adapter\extend.js:31:33)
at extend (C:\Users\piCzhang\Desktop\project_blog\node_modules\art-template\lib\compile\adapter\extend.js:31:33)
at extend (C:\Users\piCzhang\Desktop\project_blog\node_modules\art-template\lib\compile\adapter\extend.js:31:33)
at extend (C:\Users\piCzhang\Desktop\project_blog\node_modules\art-template\lib\compile\adapter\extend.js:31:33)
at extend (C:\Users\piCzhang\Desktop\project_blog\node_modules\art-template\lib\compile\adapter\extend.js:31:33)
at extend (C:\Users\piCzhang\Desktop\project_blog\node_modules\art-template\lib\compile\adapter\extend.js:31:33)
at extend (C:\Users\piCzhang\Desktop\project_blog\node_modules\art-template\lib\compile\adapter\extend.js:31:33)
at extend (C:\Users\piCzhang\Desktop\project_blog\node_modules\art-template\lib\compile\adapter\extend.js:31:33)
at extend (C:\Users\piCzhang\Desktop\project_blog\node_modules\art-template\lib\compile\adapter\extend.js:31:33)
at extend (C:\Users\piCzhang\Desktop\project_blog\node_modules\art-template\lib\compile\adapter\extend.js:31:33)

//问题描述
只有在使用populate进行多表联查才会出现这个问题,但是把主表里面的_id字段过滤掉就不会出现这个问题

//异常代码段

 const Article = require('../../model/article');
const dateFormat = require('dateformat');
const Comment = require('../../model/comment');
module.exports = async (req,res)=>{
   let article = await Article.findOne({_id:req.query.id});
   let comments = await Comment.find({aId:req.query.id}).populate('uid').lean();
  
   
  
    console.log({comments})
    res.render('home/article',{"article":article, "comments":{comments}}); //这里出错
 
}

//使用的模型Article

 const mongoose = require('mongoose');
const commentSchema = new mongoose.Schema({
 uid:{
        type:mongoose.Schema.Types.ObjectId,
        ref:'User',
        required:true
    },
    date:{
        type:Date,
        default:Date.now(),
        required:true
     },
    aId:{
        type:mongoose.Schema.Types.ObjectId,
        ref:'Article',
        required:true
    },
    content:String
});
const Comment = mongoose.model('comment',commentSchema);
module.exports = Comment;

//联查使用的模型User

const mongoose = require('mongoose');
const Joi = require('joi');

const userSchema = new mongoose.Schema({
   username: {
       type: String,
       required: true,
       minlength: 2,
       maxlength: 20
   },
   email: {
       type: String,
       unique: true,
       required: true
   },
   password: {
       type: String,
       required: true
   },
   role: {
       type: String,
       required: true

   },
   //0表示不启用,1表示启用
   state: {
       type: Number,
       default: 0
   }



});
const User = mongoose.model('User', userSchema);

const validateUser = (user) => {
   const schema = Joi.object({
       username: Joi.string()
           .alphanum()
           .min(3)
           .max(30)
           .required().error(new Error('用户名不符合规范')),

       password: Joi.string()
           .pattern(new RegExp('^[a-zA-Z0-9]{3,30}$')).error(new Error('密码不符合规范')),
       role: Joi.string()
           .pattern(new RegExp('(^admin$|^normal$)')).error(new Error('角色不符合规范'))
       ,
       state: Joi.string()
           .pattern(new RegExp('0|1')).error(new Error('状态不符合规范')),

       email: Joi.string()
           .email({ minDomainSegments: 2, tlds: { allow: ['com', 'net'] } }).error(new Error('邮箱不符合规范'))
   });
   return schema.validate(user);
}


module.exports = {
   User,
   validateUser
}
@pikzhang pikzhang changed the title express框架中使用栈溢出问题 express框架中调用render()出现栈溢出问题 Sep 10, 2020
@ghost
Copy link

ghost commented Sep 22, 2020

我也遇到这个问题,请问解决了吗?我暂时是过滤_id

@ghost
Copy link

ghost commented Sep 22, 2020

mongoose版本过高导致的,我写的版本是:
"mongoose": "^ 5.4.0", 前面的 ^ 符号,npm install 的时候实际安装的是最高5.10.0 ,我降级写 5.4.0 就不报错了

package.json文件:

"dependencies": {
"mongoose": "5.4.0",
}

@pikzhang
Copy link
Author

好的学习了,谢谢,网上还有一种方法是把Mongo对象转成普通对象,说是mongo对象内部维护的信息太多了导致爆栈,试过了这样确实没问题,不过应该还是是环境的问题,因为我的数据量非常非常低不至于爆掉栈空间

@cpeng2019
Copy link

好的学习了,谢谢,网上还有一种方法是把Mongo对象转成普通对象,说是mongo对象内部维护的信息太多了导致爆栈,试过了这样确实没问题,不过应该还是是环境的问题,因为我的数据量非常非常低不至于爆掉栈空间

谢谢! 我找了很久, 最后用你说的转对象的方法解决了, 先用了 JSON.stringify() 转成字符串, 再用了 JSON.parse() 转回对象.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants