Convert base64 string to bytes array

Hello, I am working on creating performance tests using K6. I wanted to convert a base64 string to a byte array but it’s not working as I am getting below error:
ERRO[0002] Uncaught (in promise) GoError: unable to serialise request object to protocol buffer: proto: (line 1:22): invalid value for bytes type: {
I am using below code from here to do so. Can someone please take a look?

const decBin = new Uint8Array(encoding.b64decode(enc));

Thanks for your time.

Hi @amkulkarni96 , welcome to the community forum!

The error seems to be related to gRPC and not the code you posted. Are you suing gRPC?

Can you please post more of the error and the code so I can try to better figure out what is going on.

Hi,

Below code passes data to data type defined in proto:

export default async function (ctx){
    
    let metadata = ctx.metadata;
    //console.log(metadata)

    Client.connect(BASE_URL);

    let hash=encodeStringToBytes("11100393")
    console.log(hash)
    
    const Id = 'ZhuA0OWqJZ2NeRji';
    
    const IdBytes = new Uint8Array(encoding.b64decode(Id));

    let sectionType = "SECTION_TYPE_P_SUBJECTIVE_NOTES"
    const Name = "Mary James"
  
    const LockRequestP = {
        id: IdBytes,
        section_type: sectionType,
        name: Name
    };

    // Call the gRPC methods and check their responses
    let response = Client.invoke(`${PATH}Lock`, method1, { metadata });
   
    let status_check=statusCheck(response, 'Success')
    console.log(status_check)
    console.log(response)
    if(status_check!=true){
        ctx.metadata=(await performSetup()).metadata;
        metadata = ctx.metadata;
        console.log("---------New Token Generated :", metadata)
    }

    check(response, { 'status is OK': status_check });

    response = Client.invoke(`${PATH}Lock`, method1, { metadata });
    status_check=statusCheck(response, 'Created')
    console.log(status_check)
    console.log(response)
    Client.close();
}

Datatype in proto file is as below:
message method1 {
bytes id = 1;
SectionTypeP section_type = 4;
string name = 5;
}

Also, I am using gRPC and below is more of error:

running (0m02.0s), 1/1 VUs, 24 complete and 0 interrupted iterations
Scenario_1 [>-------------------------------------] 1/1 VUs 0m01.6s/1m00.0s
ERRO[0002] Uncaught (in promise) GoError: unable to serialise request object to protocol buffer: proto: (line 1:22): invalid value for bytes type: { at reflect.methodValueCall (native)
at default (file:///C:/scripts/
/method1.js:244:48(50)) executor=ramping-vus scenario=Scenario_1

Not certain which line is 244 , which is where the error is from the exceptions, as you haven’t copied the whole test.

I can see that invoke is being called with method1 which likely is the probably the problematic value.

You also have LockRequestP which seems like what you say the proto is defined as , but you do not use it in the requests. You use method1 which if it is only the name of the definition will be undefined when used. So you should replace method1 with LockRequestP in your code.