nodejs加载模块心得,mongoose的继承,schematype的mixd介绍
2013-12-16 01:45
369 查看
1. require("xxx")可以是原生模块, 也可以是根目录“/node_modules”下的某个模块
2. 多个模块的package.json使用同一个相同模块的时候,将改公用模块写到根目录下的dependencies中,并且注意版本号,下载大家都可以公用的版本号。
若两个调用方调用的版本不一致很容易重复加载相同的模块
例如:
connect-mongo的package.json的dependencies需要"mongodb": "1.3.x",
而
mongoose的package.json的dependencies需要"mongodb": "1.3.19"
那么我们应该在根目录的dependencies中写:"mongodb": "1.3.19",
这样两者都能公用,也就不会分别下载一个不同版本的mongodb了。
3. 关于mongoose的继承问题:
http://nodejs.org/docs/latest/api/util.html
可以使用node的util.inherits方法来解决
mongoose和util.inherits联用:
https://github.com/LearnBoost/mongoose/pull/1647
4. mixed介绍:
http://mongoosejs.com/docs/schematypes.html#mixed
调用save方法的时候,mixed类型将失去更改感应的功能
因此必须在使用save方法前调用.markModified方法才能通知mongoose去保存更改
Since it is a schema-less type, you can change the value to anything else you like, but Mongoose loses the ability to auto detect and save those changes. To "tell" Mongoose that the value of a Mixed type has changed, call the
指定array类型相当于指定了mixed类型的数组
2. 多个模块的package.json使用同一个相同模块的时候,将改公用模块写到根目录下的dependencies中,并且注意版本号,下载大家都可以公用的版本号。
若两个调用方调用的版本不一致很容易重复加载相同的模块
例如:
connect-mongo的package.json的dependencies需要"mongodb": "1.3.x",
而
mongoose的package.json的dependencies需要"mongodb": "1.3.19"
那么我们应该在根目录的dependencies中写:"mongodb": "1.3.19",
这样两者都能公用,也就不会分别下载一个不同版本的mongodb了。
3. 关于mongoose的继承问题:
http://nodejs.org/docs/latest/api/util.html
可以使用node的util.inherits方法来解决
mongoose和util.inherits联用:
https://github.com/LearnBoost/mongoose/pull/1647
4. mixed介绍:
http://mongoosejs.com/docs/schematypes.html#mixed
调用save方法的时候,mixed类型将失去更改感应的功能
因此必须在使用save方法前调用.markModified方法才能通知mongoose去保存更改
Since it is a schema-less type, you can change the value to anything else you like, but Mongoose loses the ability to auto detect and save those changes. To "tell" Mongoose that the value of a Mixed type has changed, call the
.markModified(path)method of the document passing the path to the Mixed type you just changed.
指定array类型相当于指定了mixed类型的数组
相关文章推荐
- joi模块验证的介绍与使用——一种基于schema的nodejs的对象校验,简单易用
- nodejs之mongoose数据类型Schema.type
- nodejs模块加载的猜想
- Nodejs基础:stream模块入门介绍与使用
- nodejs URL模块操作URL相关方法介绍
- nodejs常用核心模块介绍
- nodejs 加载模块时,路径中的前缀“@”表示什么意思?
- nodejs中如何创建和加载模块
- nodejs的require模块(文件模块/核心模块)及路径介绍
- NodeJs模块加载机制
- xUtils3.0使用介绍:图片加载模块
- NodeJS常用模块介绍
- Nodejs进阶:MD5入门介绍及crypto模块的应用
- Nodejs之require加载机制(模块可以污染全局空间)
- nodejs 模块以及加载机制,主要讨论找不到模块的问题
- NodeJS 常用模块介绍
- nodejs模块加载机制
- nodejs 中模块使用的介绍
- Nodejs之require加载机制(模块可以污染全局空间)
- 关于require和package.loadlib的介绍和它们的关系,以及它们在加载C模块时不同表现