next.js 将获取的数据传递到POST表单时出现问题,我怎么能?

zc0qhyus  于 2023-08-04  发布在  其他
关注(0)|答案(2)|浏览(109)

该表单用于发送(从另一个API)获取的数据以及添加的注解。获取的数据很好,我将其记录到控制台。该表单似乎将获取的数据和注解正确地发送到后端,我将req.body记录到控制台。我面临的问题是我得到的回复是400。我测试通过 Postman 手动发送数据和说明的形式POST API和它的工作原理,它正确地保存到数据库.它不通过应用程序工作。

// backend/controllers/hire.js
const Vehicle = require("../models/Hire");
const { StatusCodes } = require("http-status-codes");
const { BadRequestError, NotFoundError } = require("../errors");
const cloudinary = require("../cloudinary/cloudinary");
const multer = require("multer");

const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, "uploads"); // Set the destination folder for uploaded files
  },
  filename: function (req, file, cb) {
    cb(null, Date.now() + "-" + file.originalname); // Set the filename for uploaded files
  },
});

const fileFilter = function (req, file, cb) {
  // Check file type
  if (!file.originalname.match(/\.(jpg|jpeg|png|svg)$/i)) {
    return cb(new Error("Only image files are allowed!"), false);
  }
  cb(null, true);
};

const upload = multer({ storage: storage, fileFilter: fileFilter });

const AddHire = async (req, res) => {
  console.log("backend", req.body);
  upload.array("pictures", 5)(req, res, async (error) => {
    if (error) {
      // Handle multer upload error
      if (error.message === "Unexpected field") {
        return res
          .status(StatusCodes.BAD_REQUEST)
          .json({ error: "Maximum of 5 images allowed" });
      }

      return res.status(StatusCodes.BAD_REQUEST).json({ error: error.message });
    }

    if (!req.files || req.files.length === 0) {
      // No files uploaded
      return res
        .status(StatusCodes.BAD_REQUEST)
        .json({ error: "No files uploaded" });
    }

    if (req.files.length > 5) {
      // More than 5 files uploaded
      return res
        .status(StatusCodes.BAD_REQUEST)
        .json({ error: "Maximum of 5 images allowed" });
    }

    const pictureUrls = [];

    // Upload each picture to Cloudinary and store the secure URLs
    for (const file of req.files) {
      const uniqueIdentifier =
        Date.now() + "-" + Math.round(Math.random() * 1e9);
      const publicId = `${req.user.userId}_vehicle_${uniqueIdentifier}`;

      const result = await cloudinary.uploader.upload(file.path, {
        public_id: publicId,
        width: 500,
        height: 500,
        crop: "fill",
      });

      pictureUrls.push(result.secure_url);
    }

    req.body.pictures = pictureUrls;
    req.body.createdBy = req.user.userId;

    // Extract the note from the request body
    const { note, ...vehicleDetails } = req.body;

    // Create a new vehicle entry with the user's note
    const vehicleWithNote = { ...vehicleDetails, note };

    // Store the new vehicle entry in the database
    const vehicle = await Vehicle.create(vehicleWithNote);

    res.status(StatusCodes.CREATED).json({ vehicle });
  });
};

module.exports = {
  AddHire,
};

字符串
前端

"use client";

import { useState } from "react";
import sendFormWithVehicleDetails from "@/lib/postHire";

export default function Form({ vehicle }: { vehicle: Vehicle }) {
  const {
    name,
    seat,
    price,
    description,
    type,
    pictures,
    colour,
    transmission,
  } = vehicle;
  const [note, setNote] = useState("");

  async function handleSubmit(e: React.FormEvent<HTMLFormElement>) {
    e.preventDefault();

    const vehicleDetails: FormDetails = {
      name: name,
      pictures: pictures,
      description: description,
      colour: colour,
      transmission: transmission,
      type: type,
      seat: seat,
      price: price,
    };

    // Create an object with 'note' and 'vehicleDetails'
    const dataToSend = {
      note: note,
      ...vehicleDetails,
    };

    try {
      await sendFormWithVehicleDetails(dataToSend);
      console.log("Form submitted");
    } catch (error) {
      console.error(error);
    }
  }

  return (
    <form onSubmit={handleSubmit}>
      <div className="mb-3">
        <label htmlFor="examplenote1" className="form-label">
          Add note
        </label>
        <input
          type="text"
          className="form-control"
          id="exampleInputEmail1"
          aria-describedby="textHelp"
          placeholder="Bring umbrellas"
          value={note}
          onChange={(e) => {
            setNote(e.target.value);
          }}
        />
      </div>

      <button type="submit" className="btn btn-primary">
        Submit
      </button>
    </form>
  );
}

type FormDetails = {
  name: string;
  pictures: string[];
  description: string;
  colour: string;
  transmission: string;
  type: string;
  seat: number;
  price: number;
};

import Cookies from "js-cookie";

export default async function sendFormWithVehicleDetails(
  vehicleDetails: FormDetails
) {
  const url = "http://127.0.0.1:3000/api/v1/hire";

  const token = Cookies.get("token");

  const response = await fetch(url, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      authorization: `Bearer ${token}`,
    },
    body: JSON.stringify(vehicleDetails),
  });

  if (!response.ok) {
    const errorResponse = await response.json();
    throw new Error(
      `Request failed with status ${response.status}, ${errorResponse}`
    );
  }

  const data = await response.json();
  return data;
}

drnojrws

drnojrws1#

multer处理“multipart/form-data”,但您发送的是“application/json”内容类型。

wfypjpf4

wfypjpf42#

400错误我将主体更改为表单数据类型。

import Cookies from "js-cookie";

export default async function sendFormWithVehicleDetails(formData: FormData) {
  const url = "http://127.0.0.1:3000/api/v1/hire";

  const token = Cookies.get("token");

  const response = await fetch(url, {
    method: "POST",
    headers: {
      Authorization: `Bearer ${token}`,
    },
    body: formData,
  });

  if (!response.ok) {
    const errorResponse = await response.json();
    throw new Error(
      `Request failed with status ${response.status}, ${JSON.stringify(
        errorResponse
      )}`
    );
  }

  const data = await response.json();
  return data;
}

个字符
其他一切都保持不变

相关问题