Error opening email editor TypeError: Cannot read properties of undefined (reading ‘openEmailEditor’)
Hi. i dont understand this:
this is what I did, I will not post all of my code just specifically the openEmailEditor related only:
import React, { useState, useEffect } from "react";
import { Button, hubspot } from "@hubspot/ui-extensions";
// Define the extension to be run within the Hubspot CRM
hubspot.extend(({ context, runServerlessFunction, actions }) => (
<Extension
context={context}
runServerless={runServerlessFunction}
sendAlert={actions.addAlert}
/>
));
const Extension = ({ context, runServerless, sendAlert }) => {
const [loginState, setLoginState] = useState({
token: null,
personId: null,
workEmail: null,
});
const handleSendEmail = async (email) => {
const { response } = await runServerless({
name: "openEmailEditor",
parameters: {
subject: email.scheduledEmail?.subject || "No Subject",
body: email.promptResponse?.text || "No Body Text",
to: [{ email: loginState.workEmail }],
},
});
};
return (
<Button
type="submit"
onClick={() => handleSendEmail(email)}
variant="destructive"
>
Send Email
</Button>
);
};
export default Extension;
then i created a file openEmailEditor.js:
const hubspot = require("@hubspot/api-client");
exports.main = async (context = {}) => {
console.log(context)
const { subject, body, to } = context.parameters;
try {
await context.actions.openEmailEditor({
subject,
body,
to,
});
return {
statusCode: 200,
};
} catch (error) {
console.error("Error opening email editor", error);
return {
statusCode: error,
};
}
};
and i tried to console.log the context but the result referred to event is undefined as you can see below:
{
propertiesToSend: {},
parameters: {
subject: 'Doubling leads with Union',
body: 'Hi Brian,\n' +
'\n' +
"I wanted to share how our unique approach to sales, inspired by Union's Intent Based Sales, helped us double qualified leads in just a few months. By identifying and prioritizing target audiences, we've seen amazing results. I'm curious, how are you currently approaching lead gen and integrating your marketing, sales and service teams? With your AI-powered HubSpot platform, I bet there are some powerful opportunities to explore.\n" +
'\n' +
'Best,\n' +
'John',
to: [ [Object] ]
},
event: undefined,
secrets: {}
}
Here is my serverless.json:
{
"appFunctions": {
"myFunc": {
"file": "example-function.js",
"secrets": []
},
"emailLogin": {
"file": "email-login.js",
"method": "POST",
"secrets": []
},
"fetchPlaybooks": {
"file": "fetch-playbooks.js",
"method": "GET",
"secrets": []
},
"generatePlaybook": {
"file": "generate-playbook.js",
"method": "POST",
"secrets": []
},
"fetchContact": {
"file": "fetch-contact.js",
"method": "GET",
"secrets": []
},
"fetchEmails": {
"file": "fetch-emails.js",
"method": "GET",
"secrets": []
},
"openEmailEditor": {
"file": "openEmailEditor.js",
"secrets": []
}
}
}
upon executing, i got an error here :
await context.actions.openEmailEditor
since the context.actions is undefined.