最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Waiting until readStream has ended and returning the data - Stack Overflow

programmeradmin6浏览0评论

This is my code:

downloadFile(file_id) {
    var mongoose = require('mongoose');
    var Grid = require('gridfs-stream');
    var fs = require('fs');

    mongoose.connect(config.db, {useNewUrlParser: true},).catch(e => console.log(e));
    var conn = mongoose.connection;
    Grid.mongo = mongoose.mongo;
    var gfs = Grid(conn.db);
    console.log('downloadfile', file_id);
    var read_stream = gfs.createReadStream({_id: file_id});
    let file = [];
    read_stream.on('data', function (chunk) {
        file.push(chunk);
    });
    read_stream.on('error', e => {
        console.log(e);
    });
    return read_stream.on('end', function () {
        console.log('file', file); // This logs the file buffer
        return file;
    });
}

And this is how I'm trying to use it:

Account.findById(req.params._id)
    .then(async account => {
        const file = await functions.downloadFile(account.employer.logo);
        console.log(file); // This logs the readStream data
        res.render('users/employer/booth', {
            title: 'Employer Booth',
            user: req.user,
            postings: postings,
            employer: account.employer,
            event: event,
            logo: logo,
        });
    });

And I guess it would be easy to get the downloaded file in a callback, but how can I pass this file back, and make the code execution wait?

This is how the logged file looks like file [ <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 00 fa 00 00 00 fa 08 06 00 00 00 88 ec 5a 3d 00 00 00 01 73 52 47 42 00 ae ce 1c e9 00 00 20 00 ... > ]

So to summarize. Right now, the code doesn't wait. It logs the readStream itself, and then logs the file data. How can I make it wait for the stream to end and return the file to my router?

This is my code:

downloadFile(file_id) {
    var mongoose = require('mongoose');
    var Grid = require('gridfs-stream');
    var fs = require('fs');

    mongoose.connect(config.db, {useNewUrlParser: true},).catch(e => console.log(e));
    var conn = mongoose.connection;
    Grid.mongo = mongoose.mongo;
    var gfs = Grid(conn.db);
    console.log('downloadfile', file_id);
    var read_stream = gfs.createReadStream({_id: file_id});
    let file = [];
    read_stream.on('data', function (chunk) {
        file.push(chunk);
    });
    read_stream.on('error', e => {
        console.log(e);
    });
    return read_stream.on('end', function () {
        console.log('file', file); // This logs the file buffer
        return file;
    });
}

And this is how I'm trying to use it:

Account.findById(req.params._id)
    .then(async account => {
        const file = await functions.downloadFile(account.employer.logo);
        console.log(file); // This logs the readStream data
        res.render('users/employer/booth', {
            title: 'Employer Booth',
            user: req.user,
            postings: postings,
            employer: account.employer,
            event: event,
            logo: logo,
        });
    });

And I guess it would be easy to get the downloaded file in a callback, but how can I pass this file back, and make the code execution wait?

This is how the logged file looks like file [ <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 00 fa 00 00 00 fa 08 06 00 00 00 88 ec 5a 3d 00 00 00 01 73 52 47 42 00 ae ce 1c e9 00 00 20 00 ... > ]

So to summarize. Right now, the code doesn't wait. It logs the readStream itself, and then logs the file data. How can I make it wait for the stream to end and return the file to my router?

Share Improve this question asked Nov 8, 2018 at 15:13 Alex IronsideAlex Ironside 5,07914 gold badges76 silver badges134 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

Wrap your downloadFile function into promise object and resolve only after pletely read of file and in case of error reject the promise.

Update Code

function downloadFile(file_id) {
    return new Promise((resolve, reject) => {
        var mongoose = require('mongoose');
        var Grid = require('gridfs-stream');
        var fs = require('fs');

        mongoose.connect(config.db, { useNewUrlParser: true },).catch(e => console.log(e));
        var conn = mongoose.connection;
        Grid.mongo = mongoose.mongo;
        var gfs = Grid(conn.db);
        console.log('downloadfile', file_id);
        var read_stream = gfs.createReadStream({ _id: file_id });
        let file = [];
        read_stream.on('data', function (chunk) {
            file.push(chunk);
        });
        read_stream.on('error', e => {
            reject(e);
        });
        return read_stream.on('end', function () {
            console.log('file', file); // This logs the file buffer
            resolve(file);
        });
    });
}
发布评论

评论列表(0)

  1. 暂无评论