"TypeError: GridFsStorage is not a constructor"
"TypeError: GridFsStorage is not a constructor"
出于某种原因,我不断收到“TypeError:GridFsStorage 不是构造函数”错误。我不知道为什么它会给我这个错误,因为我只是按照官方文档。
存储和上传
conn.once('open', ()=> {
gfs = Grid(conn.db, mongoose.mongo)
gfs.collection('uploads')
})
const storage = new GridFsStorage({
url: DBURI,
file: (req, file)=> {
return new Promise((resolve,reject)=> {
crypto.randomBytes(16, (err, buf)=> {
if(err) {
return reject(err)
}
const filename = buf.toString('hex') + path.extname(file.originalname);
const fileInfor = {
filename: filename,
bucketName: 'uploads'
}
resolve(fileInfo)
})
})
}
})
const upload = multer({storage})
要求
const GridFsStorage = require('multer-gridfs-storage')
错误
C:\Users\gabri\Desktop\GridFS\server.js:21
const storage = new GridFsStorage({
^
TypeError: GridFsStorage is not a constructor
at Object.<anonymous> (C:\Users\gabri\Desktop\GridFS\server.js:21:17)
at Module._compile (node:internal/modules/cjs/loader:1092:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
at Module.load (node:internal/modules/cjs/loader:972:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
at node:internal/main/run_main_module:17:47
doc on NPM 显示:
const {GridFsStorage} = require('multer-gridfs-storage');
等同于:
const GridFsStorage = require('multer-gridfs-storage').GridFsStorage;
而您使用的完全不同:
const GridFsStorage = require('multer-gridfs-storage');
使用这个:
import {GridFsStorage} from 'multer-gridfs-storage'
出于某种原因,我不断收到“TypeError:GridFsStorage 不是构造函数”错误。我不知道为什么它会给我这个错误,因为我只是按照官方文档。
存储和上传
conn.once('open', ()=> {
gfs = Grid(conn.db, mongoose.mongo)
gfs.collection('uploads')
})
const storage = new GridFsStorage({
url: DBURI,
file: (req, file)=> {
return new Promise((resolve,reject)=> {
crypto.randomBytes(16, (err, buf)=> {
if(err) {
return reject(err)
}
const filename = buf.toString('hex') + path.extname(file.originalname);
const fileInfor = {
filename: filename,
bucketName: 'uploads'
}
resolve(fileInfo)
})
})
}
})
const upload = multer({storage})
要求
const GridFsStorage = require('multer-gridfs-storage')
错误
C:\Users\gabri\Desktop\GridFS\server.js:21
const storage = new GridFsStorage({
^
TypeError: GridFsStorage is not a constructor
at Object.<anonymous> (C:\Users\gabri\Desktop\GridFS\server.js:21:17)
at Module._compile (node:internal/modules/cjs/loader:1092:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
at Module.load (node:internal/modules/cjs/loader:972:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
at node:internal/main/run_main_module:17:47
doc on NPM 显示:
const {GridFsStorage} = require('multer-gridfs-storage');
等同于:
const GridFsStorage = require('multer-gridfs-storage').GridFsStorage;
而您使用的完全不同:
const GridFsStorage = require('multer-gridfs-storage');
使用这个:
import {GridFsStorage} from 'multer-gridfs-storage'