简体   繁体   English

如何从外部 API 获取所有特定数据并发送到 Angular 中的数据库

[英]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.我是 Angular 的新手。 I want to send all selected values from a Json string coming from API to my Database.我想将来自 API 的 Json 字符串中的所有选定值发送到我的数据库。 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.我尝试将 Json 字符串转换为 Array 并使用 ForEach 循环遍历 Array 并将其等同于每个模型,但无法正常工作。 This is the format of my Json string这是我的 Json 字符串的格式

{
    "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.我想从 Json 中获取所有借方、贷方和 Trans-Date并发送到数据库。 I have tried converting to Array using Object.entries() and then loop through the Array as follows:我尝试使用Object.entries()转换为数组,然后按如下方式循环遍历数组:

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.但是没有在a.trans_date、a.credit、a.debit上选择任何值,正如我在 console.log 时所说的未定义为控制台上的值。请问有什么更好的方法来做到这一点。 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.也许您遇到了异步调用的问题,该代码可能在 API 的请求完成之前执行,因此您得到未定义的值。 If that's the case I suggest you lookup Promises or Callbacks.如果是这种情况,我建议您查找 Promises 或 Callbacks。 Can you provide the API's call from which you get this.results您能否提供从中获取 this.results 的 API 调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM