I encountered a promise error while using cypress; how can I resolve this issue?
let woID = 0
let woCoordinate = 0
let keyCloakToken = 0
class utils {
createIncidentViaAPI() {
keyCloakToken = localStorage.getItem('keycloak-token')
fetch('https://URL', {
headers: {
accept: 'application/json, text/plain, */*',
authorization: 'Bearer ' + keyCloakToken,
'content-type': 'application/json;charset=UTF-8',
},
body: '{"description":"keycloak","type":"SY","startDate":"2022-08-14T12:19:00.000Z","locationAddress":"Japanese Pagoda Ohio Dr SW","latitude":38.88366120709875,"longitude":-77.04149404953358,"sourceType":"CALL"}',
method: 'POST'
})
.then((res) => res.json())
.then((out) => {
cy.log(out.data.incidentId)
})
}
The fetch request at the top is functioning smoothly without any issues. However, I am facing challenges with the API request at the bottom.
It is crucial to note that when I send the createWorkOrderViaAPI() request, I need to wait for 60-70 seconds as the system responds every 60 seconds. Therefore, I attempted to use the then block. Despite trying different options, I have not been able to solve the promise problem.
createWorkOrderViaAPI() {
cy.request({
url: 'URL',
method: 'POST',
headers: {
properties: 'wonum',
'Content-Type': 'application/json',
MAXAUTH: 'autpassword',
Authorization: 'Basic ' + 'autpassword'
},
body: {
description: 'test request',
}
}).then((res) => {
woID = res.body.wonum
// Here, I aim to retrieve some numbers and subsequently use them in the second API request within the then block.
}).then((out)=>{
fetch('https://URL', {
headers: {
accept: 'application/json, text/plain, */*',
'accept-language': 'tr-TR,tr;q=0.9,en-US;q=0.8,en;q=0.7',
authorization: 'Bearer ' + keyCloakToken,
},
body:
'{"statusList":"sortDirection":"DESC","archivalTypeList":["ACTIVE"],"countByField":"NEIGHBORHOOD","searchText":"' +
---> I intend to use the woID number here ---> woID +
'}',
method: 'POST'
}).then((res) => {
woCoordinate = res.body.wkt
cy.log(woCoordinate)
})
})
}
Upon executing this code, I receive an error message from cypress regarding promises. The error message document is provided below:
https://i.sstatic.net/9O1hT.pngYou can also refer this link for more information.