Loopback - Return queried data as CSV file to browser

Multi tool use
Multi tool use












0















I'm querying a database table and want this dataset to be returned back to browser in CSV format i.e. in browser it should be like downloading file.



Following is my remote method which I've done so far. Don't know how to implement and what's missing.



Person.remoteMethod(
'exportPersons',
{
http: {path: '/exportPersons', verb: 'get'},
returns: [
{arg: 'Content-Type', type: 'application/octet-stream', http: { target: 'header' }}
]
}
);

Person.exportRoutes = function (cb) {

Person.find({}, function(err, data) {

var fields = ['id', 'name'];

// I'm using json2csv package
json2csv({ data: JSON.stringify(data), fields: fields }, function(err, csv) {
cb(null, csv);
});

});
}


I'm getting this error
TypeError: The header content contains invalid characters



Any kind help please !!!










share|improve this question



























    0















    I'm querying a database table and want this dataset to be returned back to browser in CSV format i.e. in browser it should be like downloading file.



    Following is my remote method which I've done so far. Don't know how to implement and what's missing.



    Person.remoteMethod(
    'exportPersons',
    {
    http: {path: '/exportPersons', verb: 'get'},
    returns: [
    {arg: 'Content-Type', type: 'application/octet-stream', http: { target: 'header' }}
    ]
    }
    );

    Person.exportRoutes = function (cb) {

    Person.find({}, function(err, data) {

    var fields = ['id', 'name'];

    // I'm using json2csv package
    json2csv({ data: JSON.stringify(data), fields: fields }, function(err, csv) {
    cb(null, csv);
    });

    });
    }


    I'm getting this error
    TypeError: The header content contains invalid characters



    Any kind help please !!!










    share|improve this question

























      0












      0








      0








      I'm querying a database table and want this dataset to be returned back to browser in CSV format i.e. in browser it should be like downloading file.



      Following is my remote method which I've done so far. Don't know how to implement and what's missing.



      Person.remoteMethod(
      'exportPersons',
      {
      http: {path: '/exportPersons', verb: 'get'},
      returns: [
      {arg: 'Content-Type', type: 'application/octet-stream', http: { target: 'header' }}
      ]
      }
      );

      Person.exportRoutes = function (cb) {

      Person.find({}, function(err, data) {

      var fields = ['id', 'name'];

      // I'm using json2csv package
      json2csv({ data: JSON.stringify(data), fields: fields }, function(err, csv) {
      cb(null, csv);
      });

      });
      }


      I'm getting this error
      TypeError: The header content contains invalid characters



      Any kind help please !!!










      share|improve this question














      I'm querying a database table and want this dataset to be returned back to browser in CSV format i.e. in browser it should be like downloading file.



      Following is my remote method which I've done so far. Don't know how to implement and what's missing.



      Person.remoteMethod(
      'exportPersons',
      {
      http: {path: '/exportPersons', verb: 'get'},
      returns: [
      {arg: 'Content-Type', type: 'application/octet-stream', http: { target: 'header' }}
      ]
      }
      );

      Person.exportRoutes = function (cb) {

      Person.find({}, function(err, data) {

      var fields = ['id', 'name'];

      // I'm using json2csv package
      json2csv({ data: JSON.stringify(data), fields: fields }, function(err, csv) {
      cb(null, csv);
      });

      });
      }


      I'm getting this error
      TypeError: The header content contains invalid characters



      Any kind help please !!!







      node.js loopbackjs loopback






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 12 '18 at 10:31









      Fakhar ZamanFakhar Zaman

      113




      113
























          1 Answer
          1






          active

          oldest

          votes


















          0














          This is how it works if you intended to return json:



          //in model.json or when we register remote method
          returns : [
          { arg: "first_property", "type" : "string"}, //first_property will be a key name
          { arg: "second_property", "type" : "string"}
          ]

          //in remote method function when we run callback
          cb( null, //error?
          first_property_value,
          second_property_value
          )


          And this is how it should be if you intended to return non-JSON result, and customize the response header:



          //in model.json or when we register remote method
          returns : [
          {"type": "string", "root": true}, //doesnt need key name
          {"arg": "Content-Type", "type": "string", "http": { "target": "header" }}, // assign the value later in the callback's third arguments
          ]

          //in remote method function when we run callback
          cb( null, //error?
          string_data,
          'application/octet-stream' // value for Content-Type header
          )


          note: loopback 3.x






          share|improve this answer
























          • another guide from the docs: loopback.io/doc/en/lb3/…

            – frendhi saido
            Dec 28 '18 at 21:40











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48744263%2floopback-return-queried-data-as-csv-file-to-browser%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          This is how it works if you intended to return json:



          //in model.json or when we register remote method
          returns : [
          { arg: "first_property", "type" : "string"}, //first_property will be a key name
          { arg: "second_property", "type" : "string"}
          ]

          //in remote method function when we run callback
          cb( null, //error?
          first_property_value,
          second_property_value
          )


          And this is how it should be if you intended to return non-JSON result, and customize the response header:



          //in model.json or when we register remote method
          returns : [
          {"type": "string", "root": true}, //doesnt need key name
          {"arg": "Content-Type", "type": "string", "http": { "target": "header" }}, // assign the value later in the callback's third arguments
          ]

          //in remote method function when we run callback
          cb( null, //error?
          string_data,
          'application/octet-stream' // value for Content-Type header
          )


          note: loopback 3.x






          share|improve this answer
























          • another guide from the docs: loopback.io/doc/en/lb3/…

            – frendhi saido
            Dec 28 '18 at 21:40
















          0














          This is how it works if you intended to return json:



          //in model.json or when we register remote method
          returns : [
          { arg: "first_property", "type" : "string"}, //first_property will be a key name
          { arg: "second_property", "type" : "string"}
          ]

          //in remote method function when we run callback
          cb( null, //error?
          first_property_value,
          second_property_value
          )


          And this is how it should be if you intended to return non-JSON result, and customize the response header:



          //in model.json or when we register remote method
          returns : [
          {"type": "string", "root": true}, //doesnt need key name
          {"arg": "Content-Type", "type": "string", "http": { "target": "header" }}, // assign the value later in the callback's third arguments
          ]

          //in remote method function when we run callback
          cb( null, //error?
          string_data,
          'application/octet-stream' // value for Content-Type header
          )


          note: loopback 3.x






          share|improve this answer
























          • another guide from the docs: loopback.io/doc/en/lb3/…

            – frendhi saido
            Dec 28 '18 at 21:40














          0












          0








          0







          This is how it works if you intended to return json:



          //in model.json or when we register remote method
          returns : [
          { arg: "first_property", "type" : "string"}, //first_property will be a key name
          { arg: "second_property", "type" : "string"}
          ]

          //in remote method function when we run callback
          cb( null, //error?
          first_property_value,
          second_property_value
          )


          And this is how it should be if you intended to return non-JSON result, and customize the response header:



          //in model.json or when we register remote method
          returns : [
          {"type": "string", "root": true}, //doesnt need key name
          {"arg": "Content-Type", "type": "string", "http": { "target": "header" }}, // assign the value later in the callback's third arguments
          ]

          //in remote method function when we run callback
          cb( null, //error?
          string_data,
          'application/octet-stream' // value for Content-Type header
          )


          note: loopback 3.x






          share|improve this answer













          This is how it works if you intended to return json:



          //in model.json or when we register remote method
          returns : [
          { arg: "first_property", "type" : "string"}, //first_property will be a key name
          { arg: "second_property", "type" : "string"}
          ]

          //in remote method function when we run callback
          cb( null, //error?
          first_property_value,
          second_property_value
          )


          And this is how it should be if you intended to return non-JSON result, and customize the response header:



          //in model.json or when we register remote method
          returns : [
          {"type": "string", "root": true}, //doesnt need key name
          {"arg": "Content-Type", "type": "string", "http": { "target": "header" }}, // assign the value later in the callback's third arguments
          ]

          //in remote method function when we run callback
          cb( null, //error?
          string_data,
          'application/octet-stream' // value for Content-Type header
          )


          note: loopback 3.x







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 28 '18 at 21:20









          frendhi saidofrendhi saido

          1




          1













          • another guide from the docs: loopback.io/doc/en/lb3/…

            – frendhi saido
            Dec 28 '18 at 21:40



















          • another guide from the docs: loopback.io/doc/en/lb3/…

            – frendhi saido
            Dec 28 '18 at 21:40

















          another guide from the docs: loopback.io/doc/en/lb3/…

          – frendhi saido
          Dec 28 '18 at 21:40





          another guide from the docs: loopback.io/doc/en/lb3/…

          – frendhi saido
          Dec 28 '18 at 21:40


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48744263%2floopback-return-queried-data-as-csv-file-to-browser%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Rp2kUnqL K 3SrIGcHsYUgVbHfpzxdpLGDJhRIEZFE4f59,VR5x9ohVW sNziRflKJVDP,L 35 MEWs91D6qmaH,YA RW3MQCDkh6kcsH
          GmoAwg0 yA,Ts

          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas