Undefined email parameters
I created actions and everything but somehow this code send an email with undefined values(in below example inputSubject is undefined).
import { emailSender } from 'wasp/server/email';
export const sendGUIMailRequest = async (inputName, inputEmail, inputSubject, inputPhone, inputEmailBody) => {
const subjectString = String(inputSubject);
try {
const info = await emailSender.send({
from: {
name: 'customer',
email: '[email protected]',
},
to: "[email protected]",
subject: subjectString,
text: "Hello world",
html: "Hello <strong>world</strong>",
});
return { success: true, info };
} catch (error) {
return { success: false, error };
}
};
If I directly set subject argument, Sengrid returns bad request error.. Like is there any example of sending mail with different arguments????
Note: If I set subject as simple string, it doesn't give error, and everything works perfectly fine.
When function is called, the arguments became undefined.
18 Replies
Hi @skyblade2954. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Based on the provided code, it seems like you are trying to send an email using Wasp's
emailSender
and you are facing an issue with the subject
field being undefined.
The emailSender.send
method accepts an object with the following fields:
- from
: The sender's details. This is an object with name
and email
fields.
- to
: The recipient's email address. This is a required string.
- subject
: The subject of the email. This is a required string.
- text
: The text version of the email. This is a required string.
- html
: The HTML version of the email. This is a required string.
In your case, the subject
field is set to subjectString
, which is the string conversion of inputSubject
. If inputSubject
is undefined
, subjectString
will also be undefined
, which might be causing the issue.
To debug this, you can try the following:
1. Check if inputSubject
is undefined
before it is passed to sendGUIMailRequest
. If it is undefined
, you need to ensure that it is defined before it is passed to the function.
2. Log subjectString
before the emailSender.send
call to see if it is undefined
. If it is undefined
, there might be an issue with the string conversion.
3. If inputSubject
is not undefined
and subjectString
is undefined
, there might be an issue with the String()
function. You can try using inputSubject.toString()
instead.
If you are still facing issues, you can refer to the Wasp documentation (https://wasp-lang.dev/docs/advanced/email#javascript-api) or ask for help in the Wasp Discord community (https://discord.com/channels/686873244791210014/1179895919471247490).
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Sorry @skyblade2954, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?Somehow, when I make a function like "sendGUIMailRequest" it only takes one input other inputs becomes undefined... when they are passed to this function..
It might be a bug, or me not knowing react well.
Anyways.
Probably the latter
Where do you get your inputSubject from? Check the value and track to the source, error speaks for itself I guess, you need to make sure you pass all the arguments
That’s not even a react question but rather a general programming
No. I am passing all the variables.
Only the first one is not passed undefined.
It is probably related with actions in this framework: https://wasp-lang.dev/docs/data-model/operations/actions
Actions | Wasp
We'll explain what Actions are and how to use them. If you're looking for a detailed API specification, skip ahead to the API Reference.
See only the first argument is passed.
Actions expect one argument - an object. You can put all your data in it
This is taken from the article you sent
Yes.
That's what I said
The problem is one should read all tutorials before starting Wasp framework, otherwise it is easy to miss.
just like with any other framework out there, you have to leran the ropes of the tool before using the tool
if you use TypeScript, it's much easier to learn, because your code editor helps you with error messages and type definitions.
Starting with a todo template for example, you get this as an example
where it is pretty apparent that a
createTask
function expects CreateArgs
, which is an object based on Pick
utility usage (and well, you can see it is destructured).
It's not so much obvious without experience and being used to tinker with things and read the code, I get that, but my point is that learning is part of any framework, not just wasp.
Wasp takes a lot of heavy lifting out of the way for you to be able to build things quickerDo you know what was easy to miss? https://wasp-lang.dev/docs/advanced/email
I am trying to implement mail system, I didn't read the tutorials. For sending mail you should add a action. And it is written in just one single comment, in this tutorial. Since it is comment. If you don't implement is as action, it fails. I had to see it from just one line of comment.
Sending Emails | Wasp
With Wasp's email-sending feature, you can easily integrate email functionality into your web application.
Since I am also new to js, it might be much harder for me.
I don’t doubt about this, but you will get there
@IamIconLiving 🌶 Thank you for picking this up, you gave all the correct answers 👍
Yes, I know that some part of it learning process, but I still believe tutorials could be writen better.
Like I said mail system must be handled in an action.
but only place that is written in that tutorial (https://wasp-lang.dev/docs/advanced/email) is: "// In some action handler..." in this comment.
If I find time, I will also make some editing on this tutorial pages...
Ofc, wasp team always accepts feedback with arms open !
Their arms are even “opener” for feedback with PRs 🙂
PR 😄
Haha, well put!