Cypress giving error about promise while sending api request, (still open)

I got a promise error by cypress what should I do, what am I missing? Can anyone help me please? If the answer is too easy I am sorry about that really I wasted serious time on it.

{I changed bodies and headers to make them clear and more readable, they were quite long and confusing.} Thank you for helping me...

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 that you can see at the top is working stable without problem but I having some issues to work API request at the bottom.

the important thing is that when I send createWorkOrderViaAPI() request I should have to wait 60-70 seconds because the system responds every 60 secs. That's why I tried to use then block. by the way, I tried some different options too but I didn't 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 want get some numbers then i want to use these second API request which you can see //inside 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 want use  woID number here ---> woID +
                    '}',
                method: 'POST'
            }).then((res) => {
                woCoordinate = res.body.wkt
                cy.log(woCoordinate)
            })
        })
    }
    

when I run this code i getting error message by cypress about promise I leaving the message doc here especially when I change cy.request to fetch error just disappears but fetch is not working not creating order as i want. enter image description here

enter image description here



Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation