如何在多部分超级代理请求中发送对象以及附加文件?

And*_*ont 5 javascript ajax node.js superagent

我正在尝试使用 superagent 向我的 API 发出多部分 POST 请求。

我的代码:

superagent
  .post(apiUrl + '/api/company/profile/edit')
  .field("profileData", profileData)
  .attach('company_logo', logoFile )
  .set('Accept', 'application/json')
  .end(function(err, res){
    if(err){
      dispatch(updateProfileStatusAction("error", res));
    } else {
      dispatch(updateProfileStatusAction("success", res));
    }
  });
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是这profileData是一个嵌套的对象。当我在 API 中收到请求时,我看到 的值profileData是字符串[Object, Object]

当我查看带有 superagent 的多部分请求的文档https://visionmedia.github.io/superagent/#multipart-requests时,它似乎.field()只是一个键、值对,而不是一个对象。然后我尝试使用 .send({profileData: profileData}) 而不是字段,但是当我这样做时,我收到一条错误消息,指出 .attach 和 .send 不能在同一请求中一起使用。

Mat*_*ano 4

JSON.stringify()我认为将 JS_Object 转换为 JSON 字符串应该足够了。

superagent
 .post(apiUrl + '/api/company/profile/edit')
 .field("profileData", JSON.stringify(profileData))
 .attach('company_logo', logoFile )
 ...
Run Code Online (Sandbox Code Playgroud)