Meteor-Files: MongoError: file with id xxx not opened for writing

Meteor-Files Version: 1.9.8 Meteor Version: 1.6.1 Issue appears on Desktop Client or Server Issues: Server throws me a MongoError and on the client side throws me service unavailable. Server Error 503 screen shot 2018-02-27 at 11 54 14 pm

Step to reproduce the problem:

// some test component.jsx

...
fetchImage = async _ => {

    const response = await methodCall ( 'files.findOne', 'file-idxxx' );

    this.setState. ( { src: Files.link ( response ) } );

}

render () {

    const { src } = this.state;

    return (
        <div>
                // click on this to simply get image link and view it, the response return just fine and i can successfully get the link, but after inject it into src, it throw me the MongoError
                <button onClick={ this.fetchImage }>fetch</button>
                <img src={ src } />
        </div>
    );

}

methods for the files.findOne

// methods.js

'files.findOne' ( _id ) {

    // tried Files.findOne ( { _id } ) and throws me a Unhandled Promised Rejection error and maximum call stack exceed
    return Files.collection.findOne ( { _id } );

}

methodCall.js

const methodCall = ( ...params ) => new Promise ( ( resolve, reject ) => {

    const method = params [ 0 ];

    store.dispatch ( methodRequest ( { method } ) );

    Meteor.call ( ...params, ( error, response ) => {

        if ( error ) {

            store.dispatch ( methodFailure ( { method, error: error.reason } ) );

            return reject ( error );

        }

        store.dispatch ( methodSuccess () );

        return resolve ( response );

    });

} );

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 24 (11 by maintainers)

Most upvoted comments

@dr-dimitru I finally found the problem. It was a very very very silly mistake that I have made and cost me days of thinking of this.

    writeStream.on ( 'close', Meteor.bindEnvironment ( uploadedFile => {

        const property = `versions.${ versionName }.meta.gridFsFileId`;

        // it should be uploadedFile._id.toString () but not file._id.toString ();
       --> change this line
        this.collection.update ( file._id.toString (), { $set: { [ property ]: file._id.toString () } } );

       --> to this line
       this.collection.update ( file._id.toString (), { $set: { [ property ]: uploadedFile._id.toString () } } ); 

        this.unlink ( this.collection.findOne ( file._id.toString () ), versionName );

    }));

After adding the new Mongo.ObjectID, it gives clearer error message that the id was not exist, and the GridFS related issues u pointed me to #341, that it exist in mongo record does not mean it exist in FS. So I went to compare again against the wiki at the afterUpload hook and then saw the mistake.

Thanks for your help and sorry for costing much of your time.

@dr-dimitru Yes, upload and view file without GridFS works fine.