因此,我使用 MongoDB Atlas、Mongoose、Multer 和 GridFsStorage 将文件上传到云上的单个数据库,效果很好,但我希望能够动态使用不同的数据库连接将文件上传到不同的数据库。
为此,我想设置 Multer GridFsStorage 配置,以便“db”属性字段根据 API 请求动态更改。但是,我无法访问“db”属性内的 HTTP 请求对象,这使得我可以动态更改数据库连接。GridFsStorage 配置对象中的
“db”属性仅接受以下内容:
在下面的示例中,您可以看到我正在使用自定义 getDatabaseConnection() 方法创建数据库连接,然后将该连接传递到接受单个静态数据库连接的数据库字段中。
// Multer GridFsStorage configuration.
const storage = new GridFsStorage({
// I NEED TO CHANGE THE DB CONNECTION DYNAMICALLY TO UPLOAD FILES TO DIFFERENT
// DATABASES, BUT I HAVE NO ACCESS TO THE HTTP REQUEST OBJECT INSIDE THE DB FIELD!
db: connectionFactory.getDatabaseConnection(process.env.DB_CONNECT),
// I can access the request object (req) inside the "file" field.
file: …Run Code Online (Sandbox Code Playgroud) 我收到以下提到的错误。基本配置如下: 我已将文件上传到服务器上,我想下载它们,但收到这些错误时,我向 /api/files/delete/${fileId} 调用了 POST 请求,该请求应该调用路由并返回文件发送到浏览器,而不是使用网格相关模块获取错误。
MONGODB_CONNECTION_STRING = mongodb://localhost:27017/Cstore
Db structure={Cstore:{uploads.files,uploads.chunks,users}}
On requesting POST axios.post(`/api/files/delete/${fileId}`)
Getting error:
this._store = new grid.mongo.GridStore(grid.db, this.id || new grid.mongo.ObjectID(), this.name, this.mode, options);
[0] ^
[0]
[0] TypeError: grid.mongo.GridStore is not a constructor
[0] at new GridReadStream (/home/lenovo/Desktop/react/cstore/node_modules/gridfs-stream/lib/readstream.js:68:17)
This is server.js
app.get('/image/:filename', (req, res) => {
gfs.files.findOne({
_id: mongoose.Types.ObjectId(req.params.filename)
// filename: req.params.filename.toString()
}, (err, file) => {
// Check if file
if (!file || file.length === 0) {
console.log("not found")
return res.status(404).json({
err: 'No file …Run Code Online (Sandbox Code Playgroud) 我是 Nodejs 和 mongoDB 的新手,任何人都可以帮我解决这个问题。我曾经使用multer、multer-gridfs-storage、gridfs-stream来上传文件和图像。但我收到此TypeError: mongodb_1.ObjectID is not a constructor in resolve() inside the Promise 中的 GridFsStorage 。我分享了下面的代码。
const express = require('express');
const app = express();
require('dotenv').config()
const PORT = process.env.PORT;
const morgan = require('morgan');
const multer = require('multer');
const {GridFsStorage} = require('multer-gridfs-storage');
const Grid = require('gridfs-stream');
const methodOverride = require('method-override');
const mongoose = require('mongoose');
const path = require('path');
const crypto = require('crypto');
const MongoURI = "atlas url";
app.use(methodOverride('_method'));
app.use(express.json())
app.use(morgan(':method :status :url')); …Run Code Online (Sandbox Code Playgroud) 使用 multer gridfs 存储上传的文件我想将它引用到新创建的集合(电影)。我引用上传文件的文件 ID 与上传文件不同,即使电影收藏没有保存到数据库中。我引用上传的文件的文件 ID 与上传的文件不同,即使电影收藏没有保存在数据库中
//movie schema
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const MovieSchema = new Schema({
description: {
type: String,
},
category: {
type: String,
},
token: {
type: String,
},
fileID: {
type: Schema.Types.ObjectId,
ref: "contents.files"
}
});
module.exports = movies = mongoose.model('movies', MovieSchema);
let gfs;
conn.once('open', () => {
gfs = Grid(conn.db, mongoose.mongo);
gfs.collection('contents');
});
const storage = new GridFsStorage({
url: config.db,
file: (req, file) => {
return new Promise((resolve, reject) …Run Code Online (Sandbox Code Playgroud)for some reason I keep getting a "TypeError: GridFsStorage is not a constructor" error. I have no idea why its giving me this error since I'm just following the official documentation.
Storage and upload
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: …Run Code Online (Sandbox Code Playgroud) 首先,我想将我的所有文件推送到heroku以用于托管目的,在这里我无法在heroku上安装multer和multer-gridfs-storage的依赖项,所以这就是为什么我无法在heroku上托管我的网站
remote: -----> Installing dependencies
remote: Installing node modules
remote: npm ERR! code ERESOLVE
remote: npm ERR! ERESOLVE could not resolve
remote: npm ERR!
remote: npm ERR! While resolving: multer-gridfs-storage@5.0.2
remote: npm ERR! Found: multer@1.4.5-lts.1
remote: npm ERR! node_modules/multer
remote: npm ERR! multer@"^1.4.5-lts.1" from the root project
remote: npm ERR!
remote: npm ERR! Could not resolve dependency:
remote: npm ERR! peer multer@"^1.4.2" from multer-gridfs-storage@5.0.2
remote: npm ERR! node_modules/multer-gridfs-storage
remote: npm ERR! multer-gridfs-storage@"^5.0.2" from the root project
remote: npm ERR!
remote: …Run Code Online (Sandbox Code Playgroud)