Reputation: 1
const mongoURI = process.env.MONGOURL;
const connection = await mongoose.createConnection(mongoURI);
connection.once('open', () => {
var gfs = Grid(conn.db, mongoose.mongo);
gfs.collection('uploads');
})
app.get('/files', (req, res) => {
var gfs = Grid(conn.db, mongoose.mongo);
gfs.collection('uploads');
if (!conn) return res.status(500).send({
message: "connect timeout",
success: false
})
if (connection) console.log("starting")
gfs.files.find().toArray((err, files) => {
console.log("in")
if (err) {
return res.status(500).json({
success: false,
message: "Error retrieving the files",
error: err
});
}
if (!files || files.length === 0) {
return res.status(400).json({
success: false,
message: "No files found"
});
}
else {
files.map(file => {
if (
file.contentType === 'image/jpeg' ||
file.contentType === 'image/png'
) {
file.isImage = true;
} else {
file.isImage = false;
}
});
res.render('dashboard', { files: files });
}
});
});
this is my code, and my toArray function is getting depricated, and if remove the var gfs line from the app.get("/files"), i am getting "cannot read properties of undefined (reading 'files')"
i refered to GridFS TypeError: Cannot read property 'files' of undefined to solve the error, but now my toArray function is getting depricated. what should i do?
Upvotes: 0
Views: 136
Reputation: 1
var gfs; conn.once('open', () => { gfs = new mongoose.mongo.GridFSBucket(conn.db, { bucketName: 'images', }); });
const file = await gfs.find({ _id }).toArray()`
try console.log the file to check
Upvotes: 0