question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MongoError: file with id xxx not opened for writing

See original GitHub issue

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 );

    });

} );

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
xxhenglyxxcommented, Mar 2, 2018

@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.

1reaction
xxhenglyxxcommented, Mar 1, 2018

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

MongoError: file with id ### not opened for writing #111 - GitHub
Need to store picture with my API using FeatherJS. I got the error MongoError: file with id ### not opened for writing. with...
Read more >
What's Mongoose error Cast to ObjectId failed for value XXX at ...
Mongoose's findById method casts the id parameter to the type of the model's ... This is an ObjectId but "foo" is not a...
Read more >
Unable to connect to Atlas cluster using "Standard ... - MongoDB
I am trying to connect to my Atlas cluster using “Standard Connection String” format. mongo "mongodb://xxx:xxx@xxx-shard-00-02.m32cu.mongodb.net ...
Read more >
WiredTiger File Forensics Part 3: Viewing all the MongoDB Data
WiredTiger doesn't name any of its data files according to the MongoDB object names, so as a first step you'll have to extract...
Read more >
MongoDB - Apache Camel
MongoDB is a very popular NoSQL solution and the camel-mongodb component integrates Camel with MongoDB allowing you to interact with MongoDB collections both...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found