简体   繁体   English

TypeError deleting a file with gridfs mongoose 和 node.js

[英]TypeError deleting a file with gridfs mongoose and node.js

I am developing an application that allows uploading and downloading music.我正在开发一个允许上传和下载音乐的应用程序。 I can upload files, send them to the client... however, I have problems when it comes to deleting a bucket file...我可以上传文件,将它们发送给客户端......但是,在删除存储桶文件时我遇到了问题......

I'am using "mongoose": "^6.2.1".我正在使用“猫鼬”:“^6.2.1”。

My controller, where podcastId is a ObjectId:我的 controller,其中 podcastId 是一个 ObjectId:

const connection = require('../database')
const mongoose = require('mongoose')
const Users = require('../models/Users')
const PodcastInfo = require('../models/PodcastInfo')

ctrPod.deletePodcast = async (req, res, next) => {

    try {
    
        const id = req.params.idPodInfo
        const info = await PodcastInfo.findById(id)
        const { userId, podcastId } = info
        const gridFsBucket = new mongoose.mongo.GridFSBucket(connection, {
        bucketName: 'podcasts',
        });

        gridFsBucket.delete(podcastId, (err) => {
            console.log(err)
        })
.
.
.


I get this error:我收到此错误:

TypeError: Cannot use 'in' operator to search for 'client' in undefined
    at getTopology

The problem appears here, \node_modules\mongodb\lib\utils.js:363:23):问题出现在这里,\node_modules\mongodb\lib\utils.js:363:23):

function getTopology(provider) {
    if (`topology` in provider && provider.topology) {
        return provider.topology;
    }
    else if ('client' in provider.s && provider.s.client.topology) {
        return provider.s.client.topology;
    }
    else if ('db' in provider.s && provider.s.db.s.client.topology) {
        return provider.s.db.s.client.topology;
    }
    throw new error_1.MongoNotConnectedError('MongoClient must be connected to perform this operation');
}
////////////////////////
 delete(id, callback) {
        return (0, utils_1.executeLegacyOperation)((0, utils_1.getTopology)(this.s.db), _delete, [this, id, callback], {
            skipSessions: true
        });
    }
/////////////////////////////////////

What am I doing wrong?我究竟做错了什么?

I think the problem lies here:我认为问题出在这里:

 const gridFsBucket = new mongoose.mongo.GridFSBucket(connection, {
        bucketName: 'podcasts',
        });

new mongoose.mongo.GridFSBucket(db,{bucketName}) takes in a db not a connection . new mongoose.mongo.GridFSBucket(db,{bucketName})接受一个db而不是一个connection Try:尝试:

const gridFsBucket = new mongoose.mongo.GridFSBucket(connection.db, {
        bucketName: 'podcasts',
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM