简体   繁体   中英

How to get all a particular Data from external API and send to Database in Angular

Good day Developers in the house. I am new in Angular. I want to send all selected values from a Json string coming from API to my Database. I have tried converting the Json string to Array and use ForEach to loop over the Array and equating it to the each of the models but is not working. This is the format of my Json string

{
    "status": "success",
    "message": "Callback successfully returned!",
    "data": {
        "transactions": [
            {
                "_id": "5fc0a7de88d2f8534f8e5a06",
                "notes": {
                    "topics": [
                        "c"
                    ],
                    "places": [
                        "c"
                    ],
                    "people": [
                        "a"
                    ],
                    "actions": [
                        "c"
                    ],
                    "subjects": [
                        "c"
                    ],
                    "prepositions": [
                        "b"
                    ],
                    "desc": "Ab aliquid sit non dolore recusandae veniam vitae."
                },
                "manual": false,
                "reconciled": false,
                "fetched": [
                    "5f8718d47c26dc18952e2c3a"
                ],
                "owner": [
                    "5f8718d47c26dc18952e2c3a"
                ],
                "record": [
                    "5fc0a7ac88d2f8534f8e59d8"
                ],
                "actions": [],
                "analyzed": [],
                "ner": {
                    "beneficiaries": "Elizabeth Ralph",
                    "account": [
                        "4066532592"
                    ]
                },
                "ner_v": 0,
                "location": {
                    "address": "159 Ms. Rosemary Severin Trafficway, Ilaje Kaduna",
                    "lat": 3.893161,
                    "long": 6.242136,
                    "raw": "Joseph Entertainments"
                },
                "branch": "JVGOPEM1",
                "cleared_date": "2020-11-26T11:15:28.641Z",
                "unformatted_cleared_date": "26-11-2020",
                "code": "c",
                "credit": null,
                "debit": 9459,
                "trans_date": "2020-11-26T19:22:29.713Z",
                "unformatted_trans_date": "26-11-2020",
                "customer": "5fa1541f6bd09b290f736608",
                "account": "5fa9e536f6b7bb837cb22bd8",
                "env": "production-sandbox",
                "bank": "5d6fe57a4099cc4b210bbeb6",
                "checked": [],
                "created_at": "2020-11-27T07:16:46.753Z",
                "last_updated": "2020-11-27T07:16:47.244Z",
                "__v": 0
            },
            {
                "_id": "5fc0a7de88d2f8534f8e5a08",
                "notes": {
                    "topics": [
                        "c"
                    ],
                    "places": [
                        "b"
                    ],
                    "people": [
                        "a"
                    ],
                    "actions": [
                        "a"
                    ],
                    "subjects": [
                        "b"
                    ],
                    "prepositions": [
                        "b"
                    ],
                    "desc": "Eligendi id eius rerum."
                },
                "manual": false,
                "reconciled": false,
                "fetched": [
                    "5f8718d47c26dc18952e2c3a"
                ],
                "owner": [
                    "5f8718d47c26dc18952e2c3a"
                ],
                "record": [
                    "5fc0a7ac88d2f8534f8e59d8"
                ],
                "actions": [],
                "analyzed": [],
                "ner": {
                    "beneficiaries": "Wilson Cletus",
                    "account": [
                        "4066538819"
                    ]
                },
                "ner_v": 0,
                "location": {
                    "address": "3992 Mr. Christina Agusto Meadow, Irepodun Enugu",
                    "lat": 8.983745,
                    "long": 8.705612,
                    "raw": "Bassey Homes"
                },
                "branch": "IGYIAWY1",
                "cleared_date": "2020-11-26T08:31:10.477Z",
                "unformatted_cleared_date": "26-11-2020",
                "code": "g",
                "credit": 99476,
                "debit": null,
                "trans_date": "2020-11-26T22:18:42.151Z",
                "unformatted_trans_date": "26-11-2020",
                "customer": "5fa1541f6bd09b290f736608",
                "account": "5fa9e536f6b7bb837cb22bd8",
                "env": "production-sandbox",
                "bank": "5d6fe57a4099cc4b210bbeb6",
                "checked": [],
                "created_at": "2020-11-27T07:16:46.753Z",
                "last_updated": "2020-11-27T07:16:47.244Z",
                "__v": 0
            },
            {
                "_id": "5fc0a7de88d2f8534f8e5a05",
                "notes": {
                    "topics": [
                        "b"
                    ],
                    "places": [
                        "a"
                    ],
                    "people": [
                        "b"
                    ],
                    "actions": [
                        "c"
                    ],
                    "subjects": [
                        "c"
                    ],
                    "prepositions": [
                        "a"
                    ],
                    "desc": "Asperiores alias quae vitae totam quia aut nisi sit."
                },
                "manual": false,
                "reconciled": false,
                "fetched": [
                    "5f8718d47c26dc18952e2c3a"
                ],
                "owner": [
                    "5f8718d47c26dc18952e2c3a"
                ],
                "record": [
                    "5fc0a7ac88d2f8534f8e59d8"
                ],
                "actions": [],
                "analyzed": [],
                "ner": {
                    "beneficiaries": "Jason Peace",
                    "account": [
                        "4066532902"
                    ]
                },
                "ner_v": 0,
                "location": {
                    "address": "3574 Ms. Festus Rockfeller Forks, Odubra Abia",
                    "lat": 7.576289,
                    "long": 7.646169,
                    "raw": "Phebian Farms"
                },
                "branch": "DAXEUYL1505",
                "cleared_date": "2020-11-27T03:29:41.423Z",
                "unformatted_cleared_date": "27-11-2020",
                "code": "a",
                "credit": null,
                "debit": 85026,
                "trans_date": "2020-11-27T00:51:09.485Z",
                "unformatted_trans_date": "27-11-2020",
                "customer": "5fa1541f6bd09b290f736608",
                "account": "5fa9e536f6b7bb837cb22bd8",
                "env": "production-sandbox",
                "bank": "5d6fe57a4099cc4b210bbeb6",
                "checked": [],
                "created_at": "2020-11-27T07:16:46.747Z",
                "last_updated": "2020-11-27T07:16:47.244Z",
                "__v": 0
            },
            {
                "_id": "5fc0a7de88d2f8534f8e5a07",
                "notes": {
                    "topics": [
                        "a"
                    ],
                    "places": [
                        "a"
                    ],
                    "people": [
                        "a"
                    ],
                    "actions": [
                        "b"
                    ],
                    "subjects": [
                        "c"
                    ],
                    "prepositions": [
                        "a"
                    ],
                    "desc": "Saepe officiis cupiditate pariatur beatae cum autem."
                },
                "manual": false,
                "reconciled": false,
                "fetched": [
                    "5f8718d47c26dc18952e2c3a"
                ],
                "owner": [
                    "5f8718d47c26dc18952e2c3a"
                ],
                "record": [
                    "5fc0a7ac88d2f8534f8e59d8"
                ],
                "actions": [],
                "analyzed": [],
                "ner": {
                    "beneficiaries": "Edith Robert",
                    "account": [
                        "4066548699"
                    ]
                },
                "ner_v": 0,
                "location": {
                    "address": "4469 Prof. Faith Ford Extension, Remo North Katsina",
                    "lat": 8.967997,
                    "long": 4.005012,
                    "raw": "Tunde Group of Schools"
                },
                "branch": "BSQISXW1",
                "cleared_date": "2020-11-26T17:19:04.997Z",
                "unformatted_cleared_date": "26-11-2020",
                "code": "v",
                "credit": 17881,
                "debit": null,
                "trans_date": "2020-11-26T08:33:24.960Z",
                "unformatted_trans_date": "26-11-2020",
                "customer": "5fa1541f6bd09b290f736608",
                "account": "5fa9e536f6b7bb837cb22bd8",
                "env": "production-sandbox",
                "bank": "5d6fe57a4099cc4b210bbeb6",
                "checked": [],
                "created_at": "2020-11-27T07:16:46.753Z",
                "last_updated": "2020-11-27T07:16:47.244Z",
                "__v": 0
            },

            {
                "_id": "5fc0a7de88d2f8534f8e5a4f",
                "notes": {
                    "topics": [
                        "b"
                    ],
                    "places": [
                        "c"
                    ],
                    "people": [
                        "c"
                    ],
                    "actions": [
                        "a"
                    ],
                    "subjects": [
                        "c"
                    ],
                    "prepositions": [
                        "b"
                    ],
                    "desc": "Labore explicabo aspernatur quidem."
                },
                "manual": false,
                "reconciled": false,
                "fetched": [
                    "5f8718d47c26dc18952e2c3a"
                ],
                "owner": [
                    "5f8718d47c26dc18952e2c3a"
                ],
                "record": [
                    "5fc0a7ac88d2f8534f8e59d8"
                ],
                "actions": [],
                "analyzed": [],
                "ner": {
                    "beneficiaries": "Marcos Rita",
                    "account": [
                        "4066621540"
                    ]
                },
                "ner_v": 0,
                "location": {
                    "address": "4847 Mrs. Jane Rockfeller Fort, Fufore Bayelsa",
                    "lat": 8.265254,
                    "long": 9.741324,
                    "raw": "Funbi Groups"
                },
                "branch": "DJMEGWO1",
                "cleared_date": "2020-11-26T10:11:10.446Z",
                "unformatted_cleared_date": "26-11-2020",
                "code": "a",
                "credit": 44971,
                "debit": null,
                "trans_date": "2020-11-26T23:04:06.130Z",
                "unformatted_trans_date": "26-11-2020",
                "customer": "5fa1541f6bd09b290f736608",
                "account": "5fa9e536f6b7bb837cb22bd8",
                "env": "production-sandbox",
                "bank": "5d6fe57a4099cc4b210bbeb6",
                "checked": [],
                "created_at": "2020-11-27T07:16:46.865Z",
                "last_updated": "2020-11-27T07:16:47.244Z",
                "__v": 0
            },
            {
                "_id": "5fc0a7de88d2f8534f8e5a66",
                "notes": {
                    "topics": [
                        "b"
                    ],
                    "places": [
                        "b"
                    ],
                    "people": [
                        "a"
                    ],
                    "actions": [
                        "c"
                    ],
                    "subjects": [
                        "b"
                    ],
                    "prepositions": [
                        "a"
                    ],
                    "desc": "Unde corporis cum adipisci aut vero dolor ea est voluptatem."
                },
                "manual": false,
                "reconciled": false,
                "fetched": [
                    "5f8718d47c26dc18952e2c3a"
                ],
                "owner": [
                    "5f8718d47c26dc18952e2c3a"
                ],
                "record": [
                    "5fc0a7ac88d2f8534f8e59d8"
                ],
                "actions": [],
                "analyzed": [],
                "ner": {
                    "beneficiaries": "Annabelle Monday",
                    "account": [
                        "4066620539"
                    ]
                },
                "ner_v": 0,
                "location": {
                    "address": "2583 Mr. Gift Ford Gardens, Osisioma Ngwa Bayelsa",
                    "lat": 3.010221,
                    "long": 3.16219,
                    "raw": "Teniola Trust Bank"
                },
                "branch": "SWUEUGF1",
                "cleared_date": "2020-11-26T11:47:10.945Z",
                "unformatted_cleared_date": "26-11-2020",
                "code": "r",
                "credit": null,
                "debit": 54068,
                "trans_date": "2020-11-26T19:14:45.986Z",
                "unformatted_trans_date": "26-11-2020",
                "customer": "5fa1541f6bd09b290f736608",
                "account": "5fa9e536f6b7bb837cb22bd8",
                "env": "production-sandbox",
                "bank": "5d6fe57a4099cc4b210bbeb6",
                "checked": [],
                "created_at": "2020-11-27T07:16:46.938Z",
                "last_updated": "2020-11-27T07:16:47.244Z",
                "__v": 0
            },
   
            {
                "_id": "5fc0a7de88d2f8534f8e5a68",
                "notes": {
                    "topics": [
                        "a"
                    ],
                    "places": [
                        "a"
                    ],
                    "people": [
                        "c"
                    ],
                    "actions": [
                        "a"
                    ],
                    "subjects": [
                        "c"
                    ],
                    "prepositions": [
                        "a"
                    ],
                    "desc": "Distinctio nihil ut impedit."
                },
                "manual": false,
                "reconciled": false,
                "fetched": [
                    "5f8718d47c26dc18952e2c3a"
                ],
                "owner": [
                    "5f8718d47c26dc18952e2c3a"
                ],
                "record": [
                    "5fc0a7ac88d2f8534f8e59d8"
                ],
                "actions": [],
                "analyzed": [],
                "ner": {
                    "beneficiaries": "Sunday Rachel",
                    "account": [
                        "4066561640"
                    ]
                },
                "ner_v": 0,
                "location": {
                    "address": "368 Prof. Mercy Rockfeller Underpass, Madagali Ondo",
                    "lat": 8.74603,
                    "long": 7.026403,
                    "raw": "Nenny Events"
                },
                "branch": "VXNESXI1",
                "cleared_date": "2020-11-26T09:14:29.626Z",
                "unformatted_cleared_date": "26-11-2020",
                "code": "y",
                "credit": null,
                "debit": 61825,
                "trans_date": "2020-11-26T21:32:21.008Z",
                "unformatted_trans_date": "26-11-2020",
                "customer": "5fa1541f6bd09b290f736608",
                "account": "5fa9e536f6b7bb837cb22bd8",
                "env": "production-sandbox",
                "bank": "5d6fe57a4099cc4b210bbeb6",
                "checked": [],
                "created_at": "2020-11-27T07:16:46.938Z",
                "last_updated": "2020-11-27T07:16:47.244Z",
                "__v": 0
            }
        ],
 
        "status": {
            "process": {
                "running": false,
                "completed": true
            }
        }
    }
}

I want to get all the debit, Credit and Trans-Date from the Json and send to database. I have tried converting to Array using Object.entries() and then loop through the Array as follows:

this.results.forEach(a => {
        let postItem: CreateDto = new CreateDto();

        postItem.transactionDate = a.trans_date;
        postItem.AmountCR = a.credit;
        postItem.AmountDR = a.debit

      });
      this._Apservices.CreateItem(postItem).pipe(finalize(()=>{this.saving = false;})).subscribe();

But is not picking any values on a.trans_date, a.credit, a.debit as is saying undefined as the value on Console when i console.log it.. Please what is the better way to do this. I need help Developers

Maybe you are having a problem with asynchronous calls, that code may be executed before the API's request was completed so you are getting undefined values. If that's the case I suggest you lookup Promises or Callbacks. Can you provide the API's call from which you get this.results

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