[英]How to map Angular 5 observable to a different format using map()
I've been searching for a few days now for this issue. 我一直在寻找这个问题的几天。 There's probably something quite obvious I'm missing out here but this stupid issue is making me quite agressive ;)
我可能在这里很想念某些东西,但是这个愚蠢的问题使我颇具攻击性;)
So here is my problem: 所以这是我的问题:
For a cross-platform app I'm making I use ionic with Angular 5. I want to have a calendar filled with meetings retrieved from an API. 对于跨平台应用程序,我正在将ionic与Angular 5结合使用。我希望日历中充满从API检索的会议。 The json returned from the API has an array of meetings.
从API返回的json有一系列会议。 Something like this:
像这样:
{
"meetings": [
{
"id": 851,
"published": false,
"title": "testtestestestestsetsetsetsetsettettt",
"title_modified": 1527688927,
etc...,
},
etc...
]
}
The problem is that the calendar needs a certain input to work. 问题在于日历需要一定的输入才能起作用。 The input it needs is:
它需要的输入是:
eventSource = {
title: ...,
startTime: ...,
endTime: ...,
allDay
}
Next I want to map json returned by the API to this $eventSource array. 接下来,我想将API返回的json映射到$ eventSource数组。 I worked my way through a lot of tutorials on how to do this with the observable returned by the Angular HttpCLient.
我通过很多教程来研究如何使用Angular HttpCLient返回的可观察对象来实现此目的。 And came up with the following:
并提出了以下内容:
private getMeetings(): void {
this.apiCtrl.get('meetings/')
.map((meetings) => {
return meetings.map((meeting) => {
return {
title: meeting.title,
startTime: meeting.start,
endTime: meeting.endTime,
allDay: false
}
})
}).subscribe(
meetings => {
this.eventSource = meetings;
console.log(this.eventSource);
}
)
}
But it returns me an error saying: meetings.map is not a functions
但是它给我返回一个错误,说:
meetings.map is not a functions
A little bit of browsing told me to add import { map } from 'rxjs/operators'
or some other import. 一点浏览就告诉我
import { map } from 'rxjs/operators'
或其他导入中添加import { map } from 'rxjs/operators'
。 But no matter what I import it doesn't work. 但是无论我导入什么都行不通。 Some said I should use the pipe() method but this also doesn't work.
有人说我应该使用pipe()方法,但这也不起作用。
I hope someone out here is able to help me formatting this json data returned by an API to the format the eventSource variable needs to display meetings in a calendar 我希望这里的人能够帮助我将API返回的json数据格式化为eventSource变量在日历中显示会议所需的格式
The ionic calendar i am using is: ionic2calendar( https://github.com/twinssbc/Ionic2-Calendar ) 我正在使用的离子日历是:ionic2calendar( https://github.com/twinssbc/Ionic2-Calendar )
You need to print out meetings
with the following line first. 您需要首先使用以下行打印
meetings
。
console.log(meetings)
Most likely meetings
is not an array. 最有可能的
meetings
不是数组。 You might have to do find a body
or some other key inside the raw return. 您可能需要在原始返回值中找到一个
body
或其他键。
Ok, should be meetings.meetings
:) 好的,应该是
meetings.meetings
:)
You need return meetings.meetings. 您需要回程meetings.meetings。 Well, change the variable to result
好吧,将变量更改为结果
private getMeetings(): void {
this.apiCtrl.get('meetings/')
.map((result) => { //put result
return result.meetings.map((meeting) => { //return result.meetings transformed
return {
title: meeting.title,
startTime: meeting.start,
endTime: meeting.endTime,
allDay: false
}
})
}).subscribe(
meetings => {
this.eventSource = meetings;
console.log(this.eventSource);
}
)
}
const respose$ = this.http.get(''); // some function returning Observable
return respose$.map(
(obj) => {
// Change Object here
obj.val1 = ""
obj.val2 = ""
}
);
// Subscribe to this Observable finally //最终订阅此Observable
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.