[英]How to get arrays/objects sent via form in sails.js (with enctype multipart/form-data)
I'm nesting information in my forms to match my models, this greatly simplify things on the backend, but I can't find out how to get arrays or objects (or the combination of both) in Sails.js 我在我的表单中嵌套信息以匹配我的模型,这大大简化了后端的内容,但我无法找到如何在Sails.js中获取数组或对象(或两者的组合)
Assuming I have a form like this 假设我有这样的表格
NOTE: Support for "multipart/form-data" is totally required. 注意:完全需要支持“multipart / form-data”。
<form action="/articles" method="post" enctype="multipart/form-data">
<input type="file" name="status" value="published">
<!-- Entry 0 -->
<input(type="text" name="entries[0][title]" value="Entry 1")
<input(type="text" name="entries[0][content]" value="Entry 1 Content...")
<!-- Entry 1 -->
<input(type="text" name="entries[1][title]" value="Entry 2")
<input(type="text" name="entries[1][content]" value="Entry 2 Content...")
<!-- images -->
<input type="file" name="images[]">
<input type="file" name="images[]">
</form>
Im expecting to get an object like this in the req.params.all() obj 我希望在req.params.all()obj中得到这样的对象
{
status: 'published',
entries: [
{title: 'Entry 1', content: 'Entry 1 Content...'},
{title: 'Entry 2', content: 'Entry 2 Content...'}
]
}
Now when calling req.params.all()
/ req.body
what I'm getting instead is: 现在当调用
req.params.all()
/ req.body
,我得到的是:
{
status: 'published',
'entries[0][title]': 'Entry 1'
'entries[0][content]': 'Entry 1 Content...'
'entries[1][title]': 'Entry 2'
'entries[1][content]': 'Entry 2 Content...'
'entries[0][title]': 'Entry 1'
}
Calling req.file('images[]')
gives the correct behavior. 调用
req.file('images[]')
会给出正确的行为。 I'm checking the ._files
property of what is returned by that fn and shows my 2 images in there. 我正在检查该fn返回的
._files
属性,并在那里显示我的2个图像。 Using the brackets here seems really weird, but that's what it is. 在这里使用括号似乎很奇怪,但这就是它的本质。
I guess that related to what I'm getting with req.params.all() I can further parse this, but it'll be hacky and brittle if something changes in the future. 我想这与我用req.params.all()得到的东西有关。我可以进一步解析这个问题,但是如果将来发生变化,那将会是hacky和脆弱的。 In any case this is a common pattern in any web app and supported by many languages and frameworks, so to me is really odd that it seems impossible to get what I need with just plain sails.js functionality, so i'm guessing that I'm not doing something as it should or that I'm missing something.
在任何情况下,这是任何Web应用程序中的常见模式,并且受到许多语言和框架的支持,所以对我来说真的很奇怪,只需简单的sails.js功能就无法获得我需要的东西,所以我猜我我没有做任何应该做的事情,或者我错过了什么。 Please point me out in the right direction, and if actually Sails does not support this basic nesting behaviour, then how should I proceed?
请指出我正确的方向,如果Sails实际上不支持这种基本的嵌套行为,那我该怎么办呢?
Sending raw content via Javascript is not an option here (Unless is impossible otherwise) as suggested in the third answer in this question: Is it possible in Sailsjs to build more complex models Doing it this way, at least for the text based fields I get the correct output, not sure with images, since I've tested via postman with rawdata. 通过Javascript发送原始内容不是一个选项(除非是不可能的),正如在这个问题的第三个答案中所建议的那样: Sailsjs中是否有可能构建更复杂的模型这样做,至少对于我得到的基于文本的字段正确的输出,不确定图像,因为我已经通过邮差用rawdata测试。
Edit: So far i've tried changing the skipper body parser in config/http.js like this: 编辑:到目前为止,我已尝试更改config / http.js中的skipper body解析器,如下所示:
bodyParser: {
fn: require('body-parser').urlencoded,
options: {extended:true}
}
But that made my server useless, it did start, but it didn't respond to any request, not sure why (Even using their our example with skipper, that you just have to uncomment, does not work). 但这使我的服务器无用,它确实启动了,但它没有响应任何请求,不知道为什么(即使使用他们的自己的示例与船长,你只需要取消注释,不起作用)。
Since skipper is based on bodyparser, I modified the skipper module index.js just to test what happens. 由于skipper基于bodysarser,我修改了skipper模块index.js只是为了测试会发生什么。
var URLEncodedBodyParser = connect.urlencoded({extended:true})
But it didn't work, I get the same result as the begining, even installing body-parser and using it instead of the connect.urlencoded body parser had no effect. 但它没有用,我得到的结果与开始相同,甚至安装了body-parser并使用它而不是connect.urlencoded体解析器没有效果。
Edit 2: As @robertklep stated using form-data without multipart works, but of course I lose the ability to upload files, which is really important and the reason I put it in the example form. 编辑2:正如@robertklep所述,使用表单数据而没有多部分工作,但当然我失去了上传文件的能力,这非常重要,我把它放在示例表单中的原因。
Edit 3: Just to complement the accepted answer in case someone needs it, this is what I did: 编辑3:只是为了补充已接受的答案以防有人需要它,这就是我所做的:
In config/http.js
在
config/http.js
middleware: {
order: [
// some middleware
'bodyParser',
'qsBodyParser',
// more middleware
],
qsBodyParser: require('../api/middleware/qsBodyParser')
}
And in api/middleware/qsBodyParser
在
api/middleware/qsBodyParser
Qs = require('qs');
qsBodyParser = function(req, res, next) {
if req.is('multipart/form-data'){
req.body = Qs.parse(req.body);
}
return next();
};
module.exports = qsBodyParser;
The currect version of skipper
depends on connect@2.25.0
, which depends on body-parser@1.6.0
, which doesn't handle the way you are sending form arrays/objects. skipper
的当前版本取决于connect@2.25.0
,它取决于body-parser@1.6.0
,它不处理发送表单数组/对象的方式。
Your form example comes out like this (using extended : true
): 您的表单示例如下所示(使用
extended : true
):
{
"entries" : [{
"content" : "Entry 1 Content..."
}, {
"content" : "Entry 2 Content..."
}]
}
The latest version ( body-parser@1.13.3
) works as expected, so you have to plug that into skipper
somehow. 最新版本(
body-parser@1.13.3
)按预期工作,所以你必须以某种方式将其插入到skipper
。
EDIT : this comment seems to suggest that using multipart/form-data
will disable array(/object?) parsing altogether. 编辑 : 这个评论似乎暗示使用
multipart/form-data
将完全禁用数组(/ object?)解析。
EDIT #2 : you can manually parse req.body
using qs
, which seems to accept an object as argument: 编辑#2 :您可以使用
qs
手动解析req.body
,它似乎接受一个对象作为参数:
var qs = require('qs');
var obj = qs.parse({
status: 'published',
'entries[0][title]': 'Entry 1',
'entries[0][content]': 'Entry 1 Content...',
'entries[1][title]': 'Entry 2',
'entries[1][content]': 'Entry 2 Content...',
});
// obj is now:
// { status: 'published',
// entries:
// [ { title: 'Entry 1', content: 'Entry 1 Content...' },
// { title: 'Entry 2', content: 'Entry 2 Content...' } ] }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.