1

I have request promise that use to call another API with few loads here's my request promise

here's my container

    if (!req.uploadedFile2) {
        opt2 = {
            method: 'POST',
            body: {
                token: req.body.token,
                name: req.body.name,
                id_type: req.body.id_type,
                id_no: req.body.id_no,
                mobile_phone: req.body.phone,
                email: req.body.email,
                address: req.body.address,
                postal_code: req.body.postal_code,
                dob: req.body.dob,
                product_code: req.body.product_code,
                billing_phone: req.body.billing_phone,
                callback_url: 'purchase',

            },
            formData: {
                images: fs.createReadStream(req.uploadPic),
            },
            uri: `${process.env.Heeey}purchase/test/test_more`,
            json: true,
        };
    }

    const obj = {
        name: req.body.name,
        id_no: req.body.id_no,
        id_type: req.body.id_type,
        mobile_phone: req.body.phone,
        birth_date: moment(req.body.dob, 'DD-MM-YYYY').format('YYYY/MM/DD'),
        postal_code: req.body.postal_code,
    };



and here's where i will send my request promise after creating dbUserContainer

    return dbUserContainer
        .create(obj)
        .then(() => rp(opt2)
            .then((results) => {
                setContent(200, results.result);
                return res.status(200).json(getContentSuccess());
            }).catch((error) => {
                setContent(400, error);
                return res.status(400).json(getContentFailOutside(error));
            }))
        .catch((error) => {
            setContent(400, error);
            return res.status(400).json(getContentFailOutside(error));
        });

when I execute this code via postman I always get this message

{
    "code": "ERR_STREAM_WRITE_AFTER_END"
}

after research, I tried to use time out to gives time for the fs.createReadStream to read and transport it here's what i have tried so far

    return dbUserContainer
        .create(obj)
        .then(() => {
            const dataRP = () => rp(opt2)
                .then((results) => {
                    setContent(200, results.result);
                    return res.status(200).json(getContentSuccess());
                }).catch((error) => {
                    setContent(400, error);
                    return res.status(400).json(getContentFailOutside(error));
                });

            setTimeout(dataRP, 2000);
        })
        .catch((error) => {
            setContent(400, error);
            return res.status(400).json(getContentFailOutside(error));
        });

but I always get but unfortunately, i get the same error message in the postman stating ERR_STREAM_WRITE_AFTER_END t the same error

0 Answers0