【问题标题】:How to add images to a database with fastify ts如何使用 fastify ts 将图像添加到数据库
【发布时间】:2022-01-30 15:13:12
【问题描述】:

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

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

【问题讨论】:

  • 堆栈跟踪在哪里?
  • 堆栈跟踪?
  • 对不起,我对后端有点陌生,有些东西看不懂哈哈
  • 堆栈跟踪是您在控制台中看到的错误。这有助于我们了解正在发生的事情
  • 哦好的,我会上传的

标签: javascript node.js typescript fastify


【解决方案1】:

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

这会解决的。

这是curl 命令:

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

【讨论】:

  • 我超开心,菜鸟失误哈哈哈
猜你喜欢
  • 2021-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-29
  • 2020-03-25
  • 2015-05-12
相关资源
最近更新 更多