Assign data in variable after population in controller

System Information
  • Strapi Version: v4.1.8
  • Operating System: Win10
  • Database: Heroku Postgres
  • Node Version: v14.19.3
  • NPM Version: 14.20.0
  • Yarn Version: 1.22.19

Hello,

I am really new to this and was thrown into cold water, maybe you can help me out on this. In my custom controller I want to send an email to a specific email address, depending on the locale of the page where the action took place. So e.g. when visiting the page in English, the mail should send to person A and if the page is in German then the mail should go to person B.

For sending the mail I am using the sendTemplatedEmail() function which works fine but right now only a static email address is working. My goal is to check a specific value which I receive from this code:

const specialist = await strapi
        .service("api::specialist.specialist")
        .findOne(response.data.id, { populate: '*' });

But I am not able to do an if else with one of the retrieved values. I already tried a few things out e.g. specialist.country.locale which doesn’t seem to work after adding this in a if/else loop after the code above, always receive a 500 Internal Server Error.

if(specialist.country.locale == "en") { ... } else { ... }

After this code part I send the email out with:

try {
        await strapi
          .plugin("email")
          .service("email")
          .sendTemplatedEmail(
            {
              to: "...@...",
            },
            emailTemplate,
            {
              specialist: {
                name: specialist.name,
                questionnaireAnswers: specialist.questionnaireAnswers,
                id: specialist.id,
              },
            }
          );
      } catch (e) {
        if (e.statusCode === 400) {
          throw new ApplicationError(e.message);
        } else {
          throw new Error(`Couldn't send email: ${e.message}.`);
        }
      }

And if I would enter specialist.country.locale instead of specialist.name I would get the right value e.g. “en”. I hope my problem is understandable and someone can help me out on this.