Home > Software engineering >  How to add images to a database with fastify ts
How to add images to a database with fastify ts

Time:01-30

I'm a little stressed because I have no idea why this error, I'm creating a mini app where people can upload their photos, but for the backend part I don't understand very well how to do that, I'm building it with fastify- ts this is what i did

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;
  }
}

Here I am creating the route to upload the image, when I upload an image it gives me an internal error, if someone can help me, I would really appreciate it :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;

postman key

This will solve.

This is the curl command:

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