javascript - 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);
});
解决方案
推荐阅读
- apache-spark - 在 spark 上下文中设置文件写入尝试次数
- c++ - 使用犰狳库时如何解决编译问题
- sqlite - 更改列表项时 Flutter ListView 不更新
- python - Heroku Django ModuleNotFoundError:没有名为“my_app”的模块
- c# - 尝试使用 c# 和 x509 删除证书时出错
- php - 为什么我不能在 Sublime Text 中使用 Xdebug 调试 PHP?
- javacard - 如何发送OTA消息?
- sequelize.js - 如何克服这个 sequelize 关联问题
- css - Vuetify 数据表组件 - 使用 CSS 截断单元格中的文本
- python - 在python中从包含标点符号的大型语料库中提取包括单词在内的句子