0

2 番目のオーディオ ファイルを gridfs に書き込む際に問題が発生しています。ng-file-upload を使用してファイルを選択し、ファイル/バイナリ データを gridfs に渡し、データベースに書き込みます。最初のファイルは正常に動作しますが、最初に別のビューを読み込んでからオーディオ ビュー/テンプレート ページに戻らない限り、毎回このエラーが発生します。

TypeError: path must be a string
    at TypeError (native)
    at Object.fs.open (fs.js:540:11)
    at ReadStream.open (fs.js:1673:6)
    at new ReadStream (fs.js:1660:10)
    at Object.fs.createReadStream (fs.js:1608:10)
    at NativeConnection.<anonymous> (Z:\techFilez\webApp\MyPiCloud\routes\IO\writeFile.js:42:29)
    at NativeConnection.g (events.js:260:16)
    at emitNone (events.js:67:13)
    at NativeConnection.emit (events.js:166:7)
    at open (Z:\techFilez\webApp\MyPiCloud\node_modules\mongoose\lib\connection.js:518:11)
    at NativeConnection.Connection.onOpen (Z:\techFilez\webApp\MyPiCloud\node_modules\mongoose\lib\connection.js:527:5)
    at Z:\techFilez\webApp\MyPiCloud\node_modules\mongoose\lib\connection.js:487:11
    at Z:\techFilez\webApp\MyPiCloud\node_modules\mongoose\lib\drivers\node-mongodb-native\connection.js:71:5
    at Z:\techFilez\webApp\MyPiCloud\node_modules\mongoose\node_modules\mongodb\lib\db.js:232:5
    at connectHandler (Z:\techFilez\webApp\MyPiCloud\node_modules\mongoose\node_modules\mongodb\lib\server.js:333:7)
    at g (events.js:260:16)
Z:\techFilez\webApp\MyPiCloud\node_modules\mongoose\node_modules\mongodb\lib\server.js:336
      process.nextTick(function() { throw err; })
                                    ^

TypeError: path must be a string
    at TypeError (native)
    at Object.fs.open (fs.js:540:11)
    at ReadStream.open (fs.js:1673:6)
    at new ReadStream (fs.js:1660:10)
    at Object.fs.createReadStream (fs.js:1608:10)
    at NativeConnection.<anonymous> (Z:\techFilez\webApp\MyPiCloud\routes\IO\writeFile.js:42:29)
    at NativeConnection.g (events.js:260:16)
    at emitNone (events.js:67:13)
    at NativeConnection.emit (events.js:166:7)
    at open (Z:\techFilez\webApp\MyPiCloud\node_modules\mongoose\lib\connection.js:518:11)
    at NativeConnection.Connection.onOpen (Z:\techFilez\webApp\MyPiCloud\node_modules\mongoose\lib\connection.js:527:5)
    at Z:\techFilez\webApp\MyPiCloud\node_modules\mongoose\lib\connection.js:487:11
    at Z:\techFilez\webApp\MyPiCloud\node_modules\mongoose\lib\drivers\node-mongodb-native\connection.js:71:5
    at Z:\techFilez\webApp\MyPiCloud\node_modules\mongoose\node_modules\mongodb\lib\db.js:232:5
    at connectHandler (Z:\techFilez\webApp\MyPiCloud\node_modules\mongoose\node_modules\mongodb\lib\server.js:333:7)
    at g (events.js:260:16)
 [nodemon] app crashed - waiting for file changes before starting...

私はいくつかのことをしました:ファイルシステムから fs.unlink(myFile) delete を使用することを確認し、ファイルの書き込みが成功するたびにマングースへの接続を閉じます。変数 var myFile = file.path (ng-file-upload からのファイル パス) を使用しています。書き込みが成功した後、myFile を null に設定して無駄にしました。

ビューを切り替えたりページを更新したりすると機能しますが、単一ページのアプリを使用するので、これは避けたいと思います.....

サーバー側の私のコードは次のとおりです。

/**
 * Created by foolishklown on 8/27/2016.
 */
var mongoose = require('mongoose'),
    path = require('path'),
    Grid = require('gridfs-stream'),
    fs = require('fs'),
    User = require('../../models/user');

module.exports = function(file, userId, fileType, res) {
    var fileId;
    //console.log('called the write file for gridfs'.green);
    //console.log('file is: ', file);
    var conn = mongoose.createConnection('mongodb://localhost/media', (error) => {
        if(error) {
            console.error('Error connecting to mongod media instance'.red);
            process.exit(1);
        } else {
            console.info('Connected successfully to mongod media instance in the write file!'.blue);
        }
    });
    // The following line is designating a file to grab/read, and save into mongo
    //  in our case it will be something from ng-file-upload that the user wants to upload
    var myFile = file.path;

    // Connect gridFs and mongo
    Grid.mongo = mongoose.mongo;

    conn.once('open', function () {
        console.log('connection open, ready for I/O!');
        var gfs = Grid(conn.db);

        // This write stream is how well write to mongo
        var writeStream = gfs.createWriteStream({
            // Name the file the way you want it stored in mongo
            filename: file.originalFilename,
            type: fileType
        });

        // Create a read stream, so that we can read its data, and then with that well use the
        //  write stream to write to the DB via piping the writestream
        var readStream = fs.createReadStream(myFile)
            .on('end', () => {
                writeToUserDb(userId, fileType, readStream.id);
                res.status(200).send({id: readStream.id, type: fileType, user: userId});
            })
            .on('error', () => {
                res.status(500).send('error in writing with gridfs');
            })
            .pipe(writeStream);

        //fs.createReadStream(myFile).pipe(writeStream);

        writeStream.on('close', function (file) {
            console.log(file.filename + 'written to DB');
            /**
            setTimeout(1000, () => {
                fs.unlink(myFile);
            });
             */
            fs.unlink(myFile);
            myFile = null;
            conn.close();
        });
    });

    function writeToUserDb(uid, type, fileId) {
        var userConn = mongoose.createConnection('mongodb://localhost/mean-auth', (error) => {
            if(error) {
                console.error('Error connecting to the mean-auth instance'.red);
                process.exit(1);
            } else {
                console.info('Connected to the mean-auth instance!'.blue);
                console.info('Attempting to find user: ' + uid + ', filetype: ' + type + ', streamID: ' + fileId + '!'.green);
                User.findById(uid, (err, doc) => {
                    if(err) {
                        console.error('Error finding user with id: ', uid);
                        process.exit(1);
                    } else {
                        console.log('original doc: ', doc);
                        doc.addMedia(type, fileId);
                        doc.save();
                        console.log('new doc: ', doc);
                    }
                })
            }
        });
        userConn.close();
    }
};
4

1 に答える 1