我正在阅读(优秀的) Sails.js一书 ,该书讨论了User.js在第6章中创建用户模型User.js :
module.exports = { connection: "needaword_postgresql", migrate: 'drop', attributes: { email: { type: 'string', email: "true", unique: 'string' }, username: { type: 'string', unique: 'string' }, encryptedPassword: { type: 'string' }, gravatarURL: { type: 'string' }, deleted: { type: 'boolean' }, admin: { type: 'boolean' }, banned: { type: 'boolean' } }, toJSON: function() { var modelAttributes = this.toObject(); delete modelAttributes.password; delete modelAttributes.confirmation; delete modelAttributes.encryptedPassword; return modelAttributes; } };使用Postgres,新记录正确填充登录表单未提交的布尔字段为null,正如本书建议的那样:
但我想使用MongoDB而不是PostgreSQL。 我没有问题切换适配器。 但是现在,当我创建一个新记录时,它似乎忽略了User.js的模式,只是将文字POST数据放入数据库中:
我知道MongoDB是NoSQL并且可以接受任何参数,但我的印象是在Users.js中使用模式将适用于对/user端点的POST请求(现在通过蓝图路由),无论数据库是什么坐在底部。 我是否需要以某种方式明确地将模型绑定到NoSQL数据库的端点?
(我已经检查了在Postgres和MongoDB中创建的记录,它们匹配来自localhost:1337 /上面发布的用户的响应)
I'm going through the (excellent) Sails.js book, which discusses creating a User model User.js in Chapter 6 like so:
module.exports = { connection: "needaword_postgresql", migrate: 'drop', attributes: { email: { type: 'string', email: "true", unique: 'string' }, username: { type: 'string', unique: 'string' }, encryptedPassword: { type: 'string' }, gravatarURL: { type: 'string' }, deleted: { type: 'boolean' }, admin: { type: 'boolean' }, banned: { type: 'boolean' } }, toJSON: function() { var modelAttributes = this.toObject(); delete modelAttributes.password; delete modelAttributes.confirmation; delete modelAttributes.encryptedPassword; return modelAttributes; } };Using Postgres, a new record correctly populates the boolean fields not submitted by the login form as null, as the book suggests should be the case:
But I want to use MongoDB instead of PostgreSQL. I had no problem switching the adaptor. But now, when I create a new record, it appears to ignore the schema in User.js and just put the literal POST data into the DB:
I understand that MongoDB is NoSQL and can take any parameters, but I was under the impression that using a schema in Users.js would apply to a POST request to the /user endpoint (via the blueprint routes for now) regardless of what database was sitting at the bottom. Do I need to somehow explicitly tie the model to the endpoint for NoSQL databases?
(I've checked the records that are created in Postgres and MongoDB, and they match the responses from localhost:1337/user posted above)
最满意答案
我知道MongoDB是NoSQL
好! 在风帆中,sails-mongo水线模块负责有关mongodb的所有事情。 我想我找到了相关的代码: https : //github.com/balderdashy/sails-mongo/blob/master/lib/document.js#L95所以sails-mongo根本不关心不存在的值。 如果您认为这很糟糕,那么请随时在github页面上创建一个问题。
可能的解决方法可能是使用defaultsTo:
banned : { type : "boolean", defaultsTo : false }I understand that MongoDB is NoSQL
Good! In sails the sails-mongo waterline module is responsible for everything regarding mongodb. I think I found the relevant code: https://github.com/balderdashy/sails-mongo/blob/master/lib/document.js#L95 So sails-mongo simply does not care about non existent values. If you think this is bad then feel free to create an issue on the github page.
A possible workaround might be using defaultsTo:
banned : { type : "boolean", defaultsTo : false }使用MongoDB时,Sails.js不应用模型方案(Sails.js not applying model scheme when using MongoDB)我正在阅读(优秀的) Sails.js一书 ,该书讨论了User.js在第6章中创建用户模型User.js :
module.exports = { connection: "needaword_postgresql", migrate: 'drop', attributes: { email: { type: 'string', email: "true", unique: 'string' }, username: { type: 'string', unique: 'string' }, encryptedPassword: { type: 'string' }, gravatarURL: { type: 'string' }, deleted: { type: 'boolean' }, admin: { type: 'boolean' }, banned: { type: 'boolean' } }, toJSON: function() { var modelAttributes = this.toObject(); delete modelAttributes.password; delete modelAttributes.confirmation; delete modelAttributes.encryptedPassword; return modelAttributes; } };使用Postgres,新记录正确填充登录表单未提交的布尔字段为null,正如本书建议的那样:
但我想使用MongoDB而不是PostgreSQL。 我没有问题切换适配器。 但是现在,当我创建一个新记录时,它似乎忽略了User.js的模式,只是将文字POST数据放入数据库中:
我知道MongoDB是NoSQL并且可以接受任何参数,但我的印象是在Users.js中使用模式将适用于对/user端点的POST请求(现在通过蓝图路由),无论数据库是什么坐在底部。 我是否需要以某种方式明确地将模型绑定到NoSQL数据库的端点?
(我已经检查了在Postgres和MongoDB中创建的记录,它们匹配来自localhost:1337 /上面发布的用户的响应)
I'm going through the (excellent) Sails.js book, which discusses creating a User model User.js in Chapter 6 like so:
module.exports = { connection: "needaword_postgresql", migrate: 'drop', attributes: { email: { type: 'string', email: "true", unique: 'string' }, username: { type: 'string', unique: 'string' }, encryptedPassword: { type: 'string' }, gravatarURL: { type: 'string' }, deleted: { type: 'boolean' }, admin: { type: 'boolean' }, banned: { type: 'boolean' } }, toJSON: function() { var modelAttributes = this.toObject(); delete modelAttributes.password; delete modelAttributes.confirmation; delete modelAttributes.encryptedPassword; return modelAttributes; } };Using Postgres, a new record correctly populates the boolean fields not submitted by the login form as null, as the book suggests should be the case:
But I want to use MongoDB instead of PostgreSQL. I had no problem switching the adaptor. But now, when I create a new record, it appears to ignore the schema in User.js and just put the literal POST data into the DB:
I understand that MongoDB is NoSQL and can take any parameters, but I was under the impression that using a schema in Users.js would apply to a POST request to the /user endpoint (via the blueprint routes for now) regardless of what database was sitting at the bottom. Do I need to somehow explicitly tie the model to the endpoint for NoSQL databases?
(I've checked the records that are created in Postgres and MongoDB, and they match the responses from localhost:1337/user posted above)
最满意答案
我知道MongoDB是NoSQL
好! 在风帆中,sails-mongo水线模块负责有关mongodb的所有事情。 我想我找到了相关的代码: https : //github.com/balderdashy/sails-mongo/blob/master/lib/document.js#L95所以sails-mongo根本不关心不存在的值。 如果您认为这很糟糕,那么请随时在github页面上创建一个问题。
可能的解决方法可能是使用defaultsTo:
banned : { type : "boolean", defaultsTo : false }I understand that MongoDB is NoSQL
Good! In sails the sails-mongo waterline module is responsible for everything regarding mongodb. I think I found the relevant code: https://github.com/balderdashy/sails-mongo/blob/master/lib/document.js#L95 So sails-mongo simply does not care about non existent values. If you think this is bad then feel free to create an issue on the github page.
A possible workaround might be using defaultsTo:
banned : { type : "boolean", defaultsTo : false }
发布评论