我正在尝试从一个可观察的响应中获取响应,对其进行过滤,并在下一个函数调用中使用该值。
这些是我的3个功能:
initTicket(options : ticket = {}, include_client : boolean ) {
let employee = this.global.getEmployee()
let submitter_id = null
let options = {}
return this.getUsers().subscribe(
success => {
let user = _.find(success['users'], { 'email': employee.email })
submitter_id = user.id
options.submitter_id = submitter_id
this.createTicket(options)
},
err => {
this.global.handleResponse(err.error, true)
}
)
}
getUsers() {
let headers = this.setOptions()
let org_id = ORG_ID;
let url = `${this.baseUrl}/organizations/${org_id}/users.json`
return this.http.get(url, {headers: headers})
}
createTicket( options : ticket = {} ) {
let headers = this.setOptions()
let data = {
"ticket": {
"subject": options.subject,
"comment": {
"body": options.comment
},
"priority": options.priority,
"group_id": options.group_id,
"type": options.type,
"tags": ['app'],
"external_id": options.external_id,
"submitter_id": 367510232431
}
}
return this.http.post(`${ this.baseUrl }/tickets.json`, JSON.stringify(data), { headers: headers })
}
这是链接两个可观察对象的正确方法还是应该使用subscribe()
以外的函数?
答案 0 :(得分:1)
我将使用switchMap
和tap
:
return this.getUsers()
.pipe(
tap((res) => {
let user = _.find(res['users'], { 'email': employee.email })
submitter_id = user.id
options.submitter_id = submitter_id
}),
switchMap(() => this.createTicket(options))
)
.subscribe(...
基本上tap
成功运行,并且switchMap
将订阅您的其他http
通话。
或者如果您没有使用最新的RXJS版本:
return this.getUsers()
.do((res) => {
let user = _.find(res['users'], { 'email': employee.email })
submitter_id = user.id
options.submitter_id = submitter_id
})
.switchMap(() => this.createTicket(options))
.subscribe(...