got error in creating pdf file using jspdf and jspdf-autotable in angular2

591 views Asked by At

I have faced a little bit error in creating pdf file in angular2, i have used the jspdf and jspdf-autotable libraries. The issue is i got the data through the node backend using angular services. the data i receive from the backend is in json format. so jspdf-autotable plugin can process it.

below is the data i get from the backend.

[{"notes":"gdsgdsgds","password":"04ba81ae04363cda785e3be93eb2e81d096d6465ec0c063d325cb48d2e1ef7511d28fd9194ae6d901ef1970d763754echXnvH8mlgzT5TK+ZdkzmEQ==","username":"gsdgdsg","folder":"dfgdf","name":"gdgfd","url":"https://www.npmjs.com/package/nodemailer"},{"notes":"test","password":"7e24572019254fecaf421fbea6a013282652ab3b43d03955292cb92b9ee011599179e4dcf7250332556a7aa38ce553fc1IWLrer/9XEr799GQ/aMPg==","username":"test","folder":"test","name":"test","url":"https://www.npmjs.com/package/nodemailer"}] 

the error which i received in console in showing below.

The data should be an object or array, is: string
ERROR TypeError: inputData.forEach is not a function
    at Object.createModels (eval at webpackJsonp.90.module.exports (addScript.js:9), <anonymous>:1220:15)
    at Object.jsPDF.API.autoTable (eval at webpackJsonp.90.module.exports (addScript.js:9), <anonymous>:2277:15)
    at SafeSubscriber._next (view-record.component.ts:92)
    at SafeSubscriber.__tryOrUnsub (Subscriber.js:238)
    at SafeSubscriber.next (Subscriber.js:185)
    at Subscriber._next (Subscriber.js:125)
    at Subscriber.next (Subscriber.js:89)
    at CatchSubscriber.Subscriber._next (Subscriber.js:125)
    at CatchSubscriber.Subscriber.next (Subscriber.js:89)
    at MapSubscriber._next (map.js:83)

i use the code from the this link https://github.com/simonbengtsson/jsPDF-AutoTable

other code is showing below

component code

exportRecords(type) {
    this.recordService.exportRecords(type).subscribe(
      (data) => {
            var columns = [
                {title: "Url", dataKey: "url"},
                {title: "Name", dataKey: "name"},
                {title: "folder", dataKey: "folder"}
                {title: "username", dataKey: "username"},
                {title: "Password", dataKey: "password"},
                {title: "Notes", dataKey: "notes"}
            ];
            var rows = data;
            // Only pt supported (not mm or in)
            var doc = new jsPDF('p', 'pt');
            doc.autoTable(columns, rows);
            doc.save('table.pdf');

            }
      },
      }

service code

exportRecords(type) {
          var headers = new Headers();
          headers.append('Content-Type', 'application/json');
          var t = localStorage.getItem("tokenKey");
          headers.append("Authorization", "Bearer " + t);
        return this.http.get('/api/record/exportRecords', {headers: headers})
        .map((res: Response) => res['_body'])
        .catch((err: Response) => Observable.throw(err.json()));
    }

router code

router.get('/exportRecords', function(req, res, next) {
   Record.find().lean().select({ "url": 1, "name": 1, "folder": 1, "username": 1, "password": 1, "notes": 1,   "_id":0}).exec(function(err, records) {
       if (err){
           console.log('err:', err)
           return res.status(400).json({errors: 'something is wrong'})
       }
       console.log('success:', records)
       return res.status(200).json(records)
   });
});

any help would be appricieated

0

There are 0 answers