是否可以在 Sailsjs 中构建更复杂的模型 [英] Is it possible in Sailsjs to build more complex models
问题描述
我想在我的模型中包含数组或集合,这是否可以使用水线 (mongoDB)?周围有其他选择吗?
I would like to have arrays or collections in my model, is this yet possible with waterline (mongoDB)? are there any alternatives around?
示例:
{
name: Bundle,
col1 : {
name : anOtherModel,
subCol: {
text: aString,
...
}
},
col2 : {
name : anOtherModel,
subCol: {
text: aString,
...
}
}
}
到:
module.exports = {
attributes : {
name : {
type : 'STRING',
required : true
},
basicModules: {
type : 'ARRAY', // or 'COLLECTION'
required : false
}
}
};
推荐答案
我不知道这是否仍然是一个问题,但诀窍是既不要将 POST 作为 "form-data" 也不要作为 "x-www-url-编码".您必须发布原始"内容:
I don't know if this is still an issue, but the trick is to neither POST as "form-data" nor "x-www-url-encoded". You have to POST the "raw" content:
假设情况:http://www.example.com/mymodel
您的标题可能如下所示:
Your Header may look like this:
POST /mymodel/create HTTP/1.1
Host: www.example.com
Cache-Control: no-cache
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="basicModules"
[1,2,3,4]
----WebKitFormBoundaryE19zNvXGzXaLvS5C
结果是字符串 "[1,2,3,4]"
得到(类型)验证,失败
the result is that a string "[1,2,3,4]"
gets (type-)validated, which fails
在这种情况下,标题是这样的:
In this case the Header is something like this:
POST /mymodel/create HTTP/1.1
Host: www.example.com
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded
basicModules=%5B1%2C2%2C3%2C4%5D
其结果与表单数据完全相同.验证失败,因为 basicModules
是字符串 "[1,2,3,4]"
which has exactly the same result as form-data. validation fails because of basicModules
being the string "[1,2,3,4]"
要使其工作,您的标题必须如下所示:
to make it work your Header has to look like this:
POST /mymodel/create HTTP/1.1
Host: www.example.com
Cache-Control: no-cache
{"basicModules":[1,2,3,4]}
这正是您想要的结果,并且类型验证有效.
which results in just exactly what you want, and type validation works.
所以最后,您可以在 JSON 中以这种方式填充最复杂的模型.例如
so in the end, you can fill the most complex models that way in JSON. e.g.
POST /mymodel/create HTTP/1.1
Host: www.example.com
Cache-Control: no-cache
{"user": {
"name": {
"first":"John",
"last":"Doe"
},
"age":25,
"pets":[{
"name":"Garfield",
"type":"cat"
},
{
"name":"Rudolph",
"type":"reindeer"
}]
}
这篇关于是否可以在 Sailsjs 中构建更复杂的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!