vue-express-mongo-boilerplate icon indicating copy to clipboard operation
vue-express-mongo-boilerplate copied to clipboard

Skip response sending from core service.

Open fizerkhan opened this issue 8 years ago • 5 comments

I want to implement download endpoint for one of the model (eg: posts). I can't do it with current implementation.

May be, you can give an option to exclude response sending for specific action. Instead that particular action send response directory using ctx.res.

fizerkhan avatar Mar 21 '17 07:03 fizerkhan

Please explain, I don't understand what would you like.

icebob avatar Mar 21 '17 08:03 icebob

In my app, I want to download contacts in CSV. Currently I can only return the data from the action, so I cannot make it downloadable. So I make a workaround by sending response from action handler itself instead of services.js

download: {
    handler(ctx) {
        return this.actions.find(ctx).
        then( (data) => {
            let fields = ['firstName', 'lastName', 'company', 'company', 'email', 'phone'];
            let fieldNames = ['First Name', 'Last Name', 'Company', 'Title',  'Email', 'Phone'];
            try {
                var result = json2csv({ data: data, fields: fields, fieldNames: fieldNames });
                ctx.res.attachment('Contacts.csv');
                ctx.res.status(200).send(result);
            } catch (err) {
                ctx.res.status(400).json({ error: { message: err.message }, status: 400 });
            }

            return C.DONT_SEND_RESPONSE;
        });
    }
},

In the core/services.js, I just skip the response sending by

  if (json === C.DONT_SEND_RESPONSE) return;

fizerkhan avatar Mar 21 '17 09:03 fizerkhan

Thanks. Now I understand. I will thinking on it how to implement correctly.

icebob avatar Mar 21 '17 20:03 icebob

In ice-services branch with Moleculer I will use this solution to handle multiple response types.

icebob avatar May 17 '17 20:05 icebob

@icebob Thats make sense 👍

fizerkhan avatar May 18 '17 05:05 fizerkhan