我有一个nodeJS服务器,它每当接收到文件时就使用multer将文件保存在MongoDB数据库中,我还想保存另一个包含已保存文件ID的文档,但是如何获取它ID ?
const conn = mongoose.createConnection(mongoURI);
conn.once("open", () => {
// Init stream
gfs = Grid(conn.db, mongoose.mongo);
gfs.collection("ProfilePic");
});
// Create storage engine
const storage = new GridFsStorage({
url: mongoURI,
file: (req, file) => {
return new Promise((resolve, reject) => {
const filename = file.originalname;
const fileInfo = {
filename: filename,
bucketName: "ProfilePic"
};
resolve(fileInfo);
});
}
});
const upload = multer({ storage });
router.post("/setProfilePic", upload.single("picture"), (req, res) => {
console.log("image received");
let possederProfilePic = await new PossederProfilePic({
_idUser = req.body.userID,
_idProfilePic = IdOfTheFileSavedPrevioulsy
})
possederProfilePic.save()
.then(user => {
res.status(201).json({ error: false });
})
.catch(err => {
res.status.json({ error: err });
});
});