简体   繁体   中英

Express error: write after end

I'm having issues with this code I'm trying to write. For some reason I get the error listed in the title. I'm pretty sure this is an issue with the async concept. How can I fix this?

pokeRouter.get('/sightings/:value([a-z]+)', function (req, res) {
var test = {};
pokemon.find().each(function(err, item) {
    if (item == null) {
        res.end();
    } else if ((req.params.value == item.type1) || (req.params.value == item.type2)) {
        sightings.find({pokedex_id: item._id}).toArray(function(err, docs) {
            if (docs == null) {
                return null;
            }
            res.write(JSON.stringify(docs));
        });
    }
});
});

Most likely you have an item in your find() list (not at the end) that is null . At that point you end the response ( res.end() ). However there are one or more items after that null item which are not null , causing a res.write() . This is what is causing your 'write after end' error. Make sure you only call res.end() after you have finished writing your entire response.

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