将实例存储在Express JS会话中 [英] Store an instance in Express js session
本文介绍了将实例存储在Express JS会话中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用expressjs会话,并且我有一个实例需要存储:
I'm using the expressjs session, and I have a instance need to store:
function a(){this.name="";}
var ins = new a();
req.session['user'] = ins;
,当我下次获得res.session ['user']时,该对象不是a的instanceof,并且它无法访问a中的任何原型方法, 我如何在会话中存储对象的引用,看起来会话会将其转换为json对象
and when I get res.session['user'] next time, the object is not instanceof a, and it cannot access any prototype methods in a, how can i store the ref of the object in session, looks the session will convert it to json object
推荐答案
除非在以下情况下,否则您不能在JSON中存储类似的实例:
You can't store instances like that in JSON unless:
- You provide your own custom serializer (implementing a
toJSON()
function in your class/object) - You also identify your serialized object and instantiate a new instance in the reviver function you pass to JSON.parse().
这篇关于将实例存储在Express JS会话中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文