#javascript #json #angular #angular8
#javascript #json #угловатый #угловой 8
Вопрос:
Добрый день, Разработчики в доме. Я новичок в Angular. Я хочу отправить все выбранные значения из строки Json, поступающей из API, в мою базу данных. Я попытался преобразовать строку Json в массив и использовать ForEach для перебора массива и приравнивания его к каждой из моделей, но не работает. Это формат моей строки 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
}
}
}
}
Я хочу получить все дебетовые, кредитные и транзакционные данные из Json и отправить в базу данных.
Я попытался преобразовать в массив с помощью 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();
Но не выбирает никаких значений в .trans_date, a.credit, a.debit, так как говорит undefined как значение на консоли, когда я консолью.запишите это в журнал..
Пожалуйста, как лучше это сделать? Мне нужна помощь Разработчиков
Комментарии:
1. Правильно ли вы просматриваете первый объект? Поскольку ‘trans_date’, credit и debit отображаются в разделе result.data.transactions, который представляет собой массив для получения желаемого результата.
2. Попробуйте this.results.data.transactions. forEach(a => { // .. })
Ответ №1:
Возможно, у вас возникли проблемы с асинхронными вызовами, этот код может быть выполнен до завершения запроса API, поэтому вы получаете неопределенные значения. Если это так, я предлагаю вам выполнить поиск обещаний или обратных вызовов. Можете ли вы предоставить вызов API, из которого вы получаете this.results
Комментарии:
1. Асинхронные вызовы завершены, потому что я консоль. запишите значения и убедитесь, что они находятся в массиве даже до a.credit , a.debit и a.trans-дата , которая говорит undefined. Также я использую условие if(), чтобы убедиться, что в результирующем массиве есть значения перед циклом ForEach