Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何在react native中从表单数据发送额外的输入值,然后在后端节点js上显示它_Node.js_Mongodb_React Native - Fatal编程技术网

Node.js 如何在react native中从表单数据发送额外的输入值,然后在后端节点js上显示它

Node.js 如何在react native中从表单数据发送额外的输入值,然后在后端节点js上显示它,node.js,mongodb,react-native,Node.js,Mongodb,React Native,您好,我正在尝试将照片上载到服务器,照片正在node js服务器上上载,但我想添加额外的字段,文件名如_id,以便我可以将照片名称更新到mongodb js,但即使在发送表单数据时,我也无法在后端获得_id 这是我的前端 const postImage = async (image) => { setIsloaded(true); console.log("Image",image) const photo = { uri: image.uri, type: "image/jpg"

您好,我正在尝试将照片上载到服务器,照片正在node js服务器上上载,但我想添加额外的字段,文件名如_id,以便我可以将照片名称更新到mongodb js,但即使在发送表单数据时,我也无法在后端获得_id

这是我的前端

const postImage = async (image) => {
setIsloaded(true);
console.log("Image",image)
const photo = {
  uri: image.uri,
  type: "image/jpg",
  name: "avatar",
  u_id:u_id
};

 const ids = {
  u_id:{u_id}
};

const data = new FormData();
data.append('file', photo);
data.append('ids', ids);

const config = {
  method: 'POST',
  body: data,
  headers: {
    Accept: 'application/json',
    // 'Content-Type': 'image/jpeg',
    'Content-Type': 'multipart/form-data',
  },
};
 fetch("https://127.0.0.1:3000/updateImageprofile", config)
 .then((responseData) => {
  console.log("Succes "+ responseData)
})
.catch((error) => {
  console.log("ERROR " + error)
});
}

这是我使用NodeJS的后端

    router.post("/updateImageprofile", upload.single('file'), async (req, res,next) => {

  console.log("body =>", req.body);
  console.log('files => ', req.files);
  console.log("file =>", req.file);
  console.log("id =>", req.ids);
    res.sendStatus(200);
  });
我的数据在控制台中显示如下

 data FormData {
  "_parts": Array [
    Array [
      "file",
      Object {
        "name": "avatar",
        "type": "image/jpg",
        "u_id": "5e831c424ca3a00b6c78311a",
        "uri": "file:///data/user/0/host.exp.exponent/cache/ExperienceData/%2540satya89310%252Fembteen/ImagePicker/05af4bc2-5883-48e0-8abb-91281b4a9b7d.jpg",
      },
    ],
    Array [
      "ids",
      Object {
        "u_id": Object {
          "u_id": "5e831c424ca3a00b6c78311a",
        },
      },
    ],
  ],
}
下面是从node js进入控制台的内容

    file => {
  fieldname: 'file',
  originalname: 'avatar',
  encoding: '7bit',
  mimetype: 'image/jpg',
  destination: 'upload/',
  filename: '39e461c3055e24921ca705efcbc48600',
  path: 'upload\\39e461c3055e24921ca705efcbc48600',
  size: 221702
}
id => undefined

id值在这里显示为空,如何获取我从前端发送的id值,在您请求的路径中是“文件”而不是“id”。尝试:

const postImage = async (image) => {
setIsloaded(true);
console.log("Image",image)
const photo = {
  uri: image.uri,
  type: "image/jpg",
  name: "avatar",
  u_id:u_id
};

 const ids = {
  u_id:{u_id}
};

const data = new FormData();
data.append('file', photo);
data.append('ids', ids);

const config = {
  method: 'POST',
  body: data,
  headers: {
    Accept: 'application/json',
    // 'Content-Type': 'image/jpeg',
    'Content-Type': 'multipart/form-data',
  },
};
 fetch("https://127.0.0.1:3000/updateImageprofile", config)
 .then((responseData) => {
  console.log("Succes "+ responseData)
})
.catch((error) => {
  console.log("ERROR " + error)
});
router.post( "/updateImageprofile", upload.any([ 'file', 'ids' ]), ...

MulterError:wrappedFileFilter(C:\react\udemy\u react\start\emb\auth\u server2\node\u modules\multer\index.js:40:19)处出现意外字段此错误显示的是带upload.any()的MMM,不带属性?body=>[对象:空原型]{}文件=>[{fieldname:'file',originalname:'avatar',encoding:'7bit',mimetype:'image/jpg',destination:'upload/',filename:'18dff3acedcd440cb90e90265d11de1',path:'upload\\18dff3aced640cb90e90265d11de1',size:162979}]file=>未定义的id=>未定义的id=>