首页 > 解决方案 > unhandledpromiserejectionwarning:错误源于在没有catch块的异步函数内部的异步机翼内部,

问题描述

它说未处理的承诺拒绝警告和崩溃。我尝试使用 node --unhandled-rejections=strict 然后它运行。但是无法将其部署到 Heroku,因为警告一次又一次地再次崩溃。我现在该怎么办?

const mongoURI =
  "mongodb+srv://shahed:shahed@cluster0.tgw1q.mongodb.net/myFirstDatabase?retryWrites=true&w=majority";

const conn = mongoose.createConnection(mongoURI, {
  useCreateIndex: true,
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

let gfs;

conn.once("open", () => {
  console.log("DB connected");

  gfs = Grid(conn.db, mongoose.mongo);
  gfs.collection("images");
});

const storage = new GridFsStorage({
  url: mongoURI,
  file: (req, file) => {
    return new Promise((resolve, reject) => {
      const fileName = `image-${Date.now()}${path.extname(file.originalname)}`;

      const fileInfo = {
        fileName: fileName,
        bucketName: "images",
      };
      resolve(fileInfo);
    });
  },
});

const upload = multer({ storage });

mongoose.connect(mongoURI, {
  useCreateIndex: true,
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

app.get("/", (req, res, next) => {
  res.status(200).send("Hello there");
});

app.post("/upload/image", upload.single("file"), (req, res) => {
  res.status(201).send(req.file);
});

标签: javascriptnode.js

解决方案


推荐阅读