Pass Variable from one function to another in load test using K6

New to K6, for benchmarking my http request I am using K6 in my pipeline. Now here is my code which have two section

first section is responsible for creating an user and second is trying updating the same user using a profileID which is generated as a part of first request.

import http from "k6/http";
import { check, sleep, group } from "k6";
import { Trend } from "k6/metrics";

import { LOGIN_URL, BASE_URL } from "../test/support/constants/endpoints.js";
import {
  generateSampleUserDataFinal,
  generateRandomPhoneNumber,
  generateRandomOrganisationContactID,
  generateRandomUserTypeId,
} from "../test/support/data/perf.sampledata.js";


let TC01_CreateNewUser = new Trend("TC01_CreateNewUser");
let TC02_UpdateExistingUser = new Trend("TC02_UpdateExistingUser");

function login() {
  let authResponse = http.post(`${LOGIN_URL}`);
  let authData = authResponse.json().data;
  if (!authData) {
    console.error("Login failed: ", authResponse.body);
  }
  return authData;
}

function generateRandomSleepTimer() {
  let sleepTime = Math.floor(Math.random() * 3 + 1);
  return parseInt(sleepTime);
}


export function createNewUser(
  authToken,
  sleepTime,
  generateSampleUserData
) {
  group("TC01: Create a new User", function () {
    const response = http.post(
      `${BASE_URL}/User`,
      generateSampleUserData,
      {
        headers: {
          Authorization: authToken,
          "Content-Type": "application/json",
        },
        tags: { name: "Create a new User" },
      }
    );
    const responseDuration = response.timings.duration;
    TC01_CreateNewUser.add(responseDuration);
    console.log("Response: ", response.body);
    profileId = JSON.parse(response.body).data;
    const checkResult = check(response, {
      "is status 200": (r) => r.status === 200,
    });
    let responseMessage;
    try {
      responseMessage = response.json();
    } catch (e) {
      console.error("Failed to parse JSON response: ", response.body);
    }
    check(responseMessage, {
      "Success!": (r) => r && r.message === "Success!",
    });
    sleep(sleepTime);
   return  profileId ;
  });
}


 export function updateExistingUser(authToken, sleepTime, profileId) {
   group("TC02: Update an existing User", function () {
      console.log("Profile ID: ====> : ", profileId);
      const response = http.put(
       `${BASE_URL}/User`,
       {
         UserProfileID: `${profileId}`,
         organisationContactID: `${generateRandomOrganisationContactID()}`,
         name: "Performnace Benchmark Testing",
         UserTypeID: `${generateRandomUserTypeId()}`,
         email: "[email protected]",
         phoneNumber: `${generateRandomPhoneNumber()}`
       },
       {
         headers: {
           Authorization: authToken,
           "Content-Type": "application/json",
         },
         tags: { name: "Update an existing User" },
       }
     );
     const responseDuration = response.timings.duration;
     TC02_UpdateExistingUser.add(responseDuration);
     console.log("Response: ", response.body);
     const checkResult = check(response, {
       "is status 200": (r) => r.status === 200,
     });
     let responseMessage;
     try {
       responseMessage = response.json();
     } catch (e) {
       console.error("Failed to parse JSON response: ", response.body);
     }
     check(responseMessage, {
       "Success!": (r) => r && r.message === "Success!",
     });
     sleep(sleepTime);
   });
 }
 
 
 export default function () {
  const authToken = login();
  if (!authToken) {
    console.error("No auth token, exiting test");
    return;
  }
  const sleepTime = generateRandomSleepTimer();
  const UserProfileID = generateRandomUserProfileID();
  let generateSampleUserData = generateSampleUserDataFinal(); //This is function which generate a random payload
  let profileId;

  createNewUser(authToken, sleepTime, generateSampleUserData);
  updateExistingUser(authToken, sleepTime, profileId);
}

Getting response for first section

INFO[0001] Response:  {"responseCode":200,"message":"Success!","data":"372"}  source=console
INFO[0002] Response:  {"responseCode":200,"message":"Success!","data":"373"}  source=console
INFO[0003] Response:  {"responseCode":200,"message":"Success!","data":"374"}  source=console

but for second I am getting

INFO[0003] Profile ID: ====> undefined           source=console 

How to fix this issue ? How I can user data handler to pass data between the function. In JMeter usually I can use a post processer to capture the value and use it further.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật