I am trying to save a token from DB and pass the token in the next API request in the body I have written the below code but it's not working, please help.
it.only('Change Password', () => {
cy.ResetPassAPI(globalThis.data.dradminemail);// this generates a token in DB
const resetoken = cy.task(
'queryDb',
`select _Token from tokens WHERE _Member_Id = 25372 and _Token_Type = "resetPassword" ORDER BY _id DESC LIMIT 1`,); // saving the token from DB generate from above
cy.request({
method: 'PATCH',
url: 'https://xxx/xx/xx/changePasswordWithToken',
body: {
confirmPassword: 'xxx',
password: 'xx',
token: resetoken, //passing the saved token from database
uid: 1234,
},
});
});
You have the wrong syntax for cy.task()
Instead of
const resetoken = cy.task(...)
it should be
cy.task(...).then(resetoken => {
Full test:
it('Change Password', () => {
cy.ResetPassAPI(globalThis.data.dradminemail);
const query = 'select _Token from tokens WHERE _Member_Id = 25372 and _Token_Type = "resetPassword" ORDER BY _id DESC LIMIT 1';
cy.task('queryDb', query)
.then(queryResult => {
const resetoken = queryResult._Token;
cy.request({
method: 'PATCH',
url: 'https://xxx/xx/xx/changePasswordWithToken',
body: {
confirmPassword: 'xxx',
password: 'xx',
token: resetoken,
uid: 1234,
},
});
});
});
You probably don't need the token anywhere else, once the PATCH has been sent.