Reputation: 6684
I am trying to take the response from one observable, filter it, and use that value in the next function call.
These are my 3 functions:
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 })
}
Is this the correct way to chain the 2 observables or should I be using a function other than subscribe()
?
Upvotes: 0
Views: 61
Reputation: 28348
I'd use switchMap
and 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(...
Basically tap
runs on success and switchMap
will subscribe to your other http
call.
Or if you're not on the latest RXJS version:
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(...
Upvotes: 1