简体   繁体   中英

Error during synchronous write using Node.js

I have written code that parses a dictionary returned from Firebase containing images encoded using base64. I want it to simply write these images to file, and it does, but I receive the following error after my writes finish:

smalloc.cc:280: void node::smalloc::SliceOnto(const v8::FunctionCallbackInfo<v8::Value>&): Assertion `end <= source_len' failed.

This is my code:

    // Iterate through each key in each page if each request
for (var key in request) {

    var obj = request[key];

    if (typeof (obj) == "object") {

        for (var prop in obj) {

            item++;
            if(obj.hasOwnProperty(prop)) {

                switch (prop) {

                    case "img":

                        var media = new ReceivedMedia(obj[prop]);

                        var filename = transaction.tid + "-" + item + "." + media.extension;
                        filename = filename.slice(10);
                        require('fs').writeFileSync(filename, media.b64, 'base64', function(err) {
                            if (err) throw err;
                        });
                        break;
                }
            }
        }
    }
}

My images come out fine, but the error is a little weird and I would prefer to not to occur. Would anyone have an idea as to why this is happening? That would be super helpful :)

Note: ReceivedMedia is a class I defined as:

function ReceivedMedia(media) {
    this.b64 = media.b64;
    this.extension = media.extension;
    this.posx = media.posx;
    this.posy = media.posy;
}

Side question: If I use writeFile instead of writeFileSync one of my images is corrupted and the other contains no data. If after that happens I run my node script again, the files save correctly. I would also like some explanation as to why that happens, from my understanding one of these is synchronous ( writeFileSync I am guessing) and the other is asynchronous ( writeFile I am assuming).

A Google search for your error message description found this discussion of the issue in io.js and this discussion in node.js and it sounds like it is a bug that has been fixed (not sure if the fix has been released in a full build yet).

The node.js fix is here .

If you were desparate to fix it now in your own build, you'd have to apply the fix to your own code tree and rebuild it. Otherwise, you'll have to investigate or inquire when this fix will get into an official release (I'm not personally sure how that process works for node.js).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM