Create login and register function to another application

Hello, i am trying to build login and register function to my application. The software i use for develop my application only can connect with database. So, i use http endpoint mongodb. But I shown error when i am trying to connect with my application. I am very new to this. Anyone can help.

This is my function for auth in the http endpoints:

 exports = async function(loginPayload) {
  // Get a handle for the app.users collection
  const users = context.services
  .get("Syndes")
  .db("account")
  .collection("users");
  
  //console.log(loginPayload);
  const username = loginPayload.toString();

  const user = await users.findOne( {"userData.username": username} );

  if (user) {
    // If the user document exists, return its unique ID
    return user._id.toString();
  } else {
    // If the user document does not exist, create it and then return its unique ID
    const newDocument = {
      "userData": {
        "username": username,
        "email": "",
        "password":" "
      }
    };
    
    const result = await users.insertOne(newDocument);
    
    return result.insertedId.toString();
  }
};

and this is the error message i got:

 {"error":"SyntaxError: invalid character 'u' looking for beginning of value","error_code":"FunctionExecutionError","link":"https://realm.mongodb.com/groups/630d71853be32c526f524ebe/apps/630d7789b5b628f76fcf2edd/logs?co_id=631af32e3c46216516bff9a4"}

Did you ever solve this?