如何使用 fastify ts 将图像添加到数据库

How to add images to a database with fastify ts

我有点压力,因为我不知道为什么会出现这个错误,我正在创建一个迷你应用程序,人们可以在其中上传他们的照片,但对于后端部分,我不太了解如何做到这一点,我正在用 fastify 构建它- ts 这就是我所做的

import fp from "fastify-plugin";
import fastifySocket from "fastify-socket.io";
import SocketConnect from "../events/connections";
import fastifyCors from "fastify-cors";
import multer from "fastify-multer";
import { File, FilesObject } from "fastify-multer/lib/interfaces";

type FilesInRequest = FilesObject | Partial<File>[];

export interface SupportPluginOptions {}

export default fp<SupportPluginOptions>(async (fastify, opts) => {
  fastify.decorate("someSupport", function () {
    return "hugs";
  });

  void fastify.register(fastifySocket);
  void fastify.register(fastifyCors);
  void fastify.register(SocketConnect);

  void fastify.register(multer.contentParser);

});

declare module "fastify" {
  export interface FastifyInstance {
    someSupport(): string;

  }
}

declare module "fastify" {
  export interface FastifyRequest {
    files: FilesInRequest;
  }
}

这里我正在创建上传图片的路由,当我上传图片时它给我一个内部错误,如果有人能帮助我,我将不胜感激:DDD

import { FastifyPluginAsync } from "fastify";
import multer from "fastify-multer";

const upload = multer({ dest: "upload" });

const publication: FastifyPluginAsync = async (fastify, options) => {
  fastify.post(
    "/create",
    {
      preHandler: upload.single("avatar"),
    },
    async (request, reply) => {
      const { files } = request;

      console.log(files);
      return reply.code(200).send("SUCCESS");
    }
  );
};

export default publication;

bug photo

这是我正在做的项目的git,大家看看:

bug Photo Two

这是我用邮递员的例子

bug postman One

///

Bug postman Two

https://github.com/TioElvis/RedSocial

upload.single("avatar"), 语句表示您需要一个 avatar 字段,但在您的邮递员电话中您没有在正文选项卡中设置 KEY 值。

这就解决了。

这是 curl 命令:

curl --location --request GET 'http://localhost:8080/publication/create' \
--form 'avatar=@"/Pictures/60iu3g.gif"'