FileNotFound - при попытке получить файл с помощью mongoose-gridfs - PullRequest
0 голосов
/ 28 марта 2019

Я пытаюсь создать метод получения, который получает поток из базы данных,

var mongoose = require('mongoose');
var gridfs = require('mongoose-gridfs');
var express = require('express');
var router = express.Router();
var fs = require('fs');


mongoose.connection.on('open', function(){
    const { model: Attachment } = gridfs({
        collection: 'attachments',
        model: 'Attachment',
        mongooseConnection: mongoose.connection
      });

      router.post('/', function(req,res){

        const readStream = fs.createReadStream(req.body.path);
        var split = req.body.path.split('/');

        const options = ({ filename: split[split.length-1] , contentType: 'binary/octet-stream' });

        Attachment.write(options, readStream, (error, file) => {
            if(error)console.log(error);
            res.json(file._id);
        });
    });

   router.get('/', function(req,res){
      Attachment.findById(req.body.id, (error, attachment) => { 
        if(error) console.log(error);
        console.log(attachment);

     });
      Attachment.readById(req.body.id, (error, attachment) => { 
        if(error) console.log(error);
        console.log(attachment);

     });

      res.json(200)
})
})

, поэтому я могу записать в базу данных, но по какой-то причине я не могу получить файлоднако через метод readbyId я могу сделать findById для того же объекта.

Вывод из FindById

   { aliases: [],
  _id: 5c9cda6253479f4fd4473581,
  length: 17474,
  chunkSize: 261120,
  uploadDate: 2019-03-28T14:29:54.684Z,
  filename: 'testphoto.PNG',
  md5: 'a7d86bd3ba3a889a4c99844fb4287fb8',
  contentType: 'binary/octet-stream' }

из ReadById

Error: FileNotFound: file 5c9cda6253479f4fd4473581 was not found

Как видите, идентификатор можно найти с помощью метода FindById, но не с помощью метода ReadById.Здесь вы найдете пакет, который я использую:

https://www.npmjs.com/package/mongoose-gridfs

, если вам нужна дополнительная информация, просто дайте мне знать, и я отредактирую ее как можно скорее.Заранее спасибо.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...