NodeJS function on AWS Lambda with mjml returns undefined - email

I´m implementing an Serverless stack on AWS with a Lambda function that submits an email formatted with MJML library in HTML format.
When trying to isolate the issue, I´ve created a simple function with the next code that runs on AWS lambda and it on itself doesn´t run (the rest does):
import mjml2html from 'mjml';
export async function sendEmail(){
console.log("sendEmail reached");
return mjml2html(`
<mjml>
<mj-body>
<mj-section>
<mj-column>
<mj-image width="100px" src="/assets/img/logo-small.png"></mj-image>
<mj-divider border-color="#F45E43"></mj-divider>
<mj-text font-size="20px" color="#F45E43" font-family="helvetica">Hello World</mj-text>
</mj-column>
</mj-section>
</mj-body>
</mjml>
`);
Even with this simple function, when calling the AWS lambda, I always get the following error and the console.log() isn´t executing, so I assume the error happens already at the import time:
"errorType": "TypeError",
"errorMessage": "Cannot read property '1' of undefined",
"stack": [
"TypeError: Cannot read property '1' of undefined",
" at isFileType (fs.js:178:19)",
" at Object.readFileSync (fs.js:367:16)",
" at /var/task/src/handlers/webpack:/uglify-js/tools/node.js:20:19",
" at Array.map (<anonymous>)",
" at /var/task/src/handlers/webpack:/uglify-js/tools/node.js:19:30",
" at Object.7199 (/var/task/src/handlers/webpack:/uglify-js/tools/node.js:24:1)",
" at ni (/var/task/src/handlers/webpack:/webpack/bootstrap:19:32)",
" at Object.56069 (/var/task/src/handlers/webpack:/html-minifier/src/htmlminifier.js:8:16)",
" at ni (/var/task/src/handlers/webpack:/webpack/bootstrap:19:32)",
" at Object.1510 (/var/task/src/handlers/webpack:/mjml-core/lib/index.js:100:21)"
]
}
I´ve looked for info on the same issue in mjml repo, mjml documentation, this forum... but no luck.
Could anybody help me identify the issue?
Thanks a lot.

Related

How to access custom header from AWS Lambda Authorizer?

I have created an Authorizer in AWS API Gateway. This Authorizer refers to a Lambda Function.
I am passing the following values in header, to the API Endpoint using Postman.
{
"type":"TOKEN",
"authorizationToken": "testing2",
"methodArn": "arn:aws:execute-api:us-west-2:444456789012:ymy8tbxw7b/*/GET/"
}
The above header values are received in the Lambda Function. I can see this through the logs in CloudWatch.
I want to pass additional value 'clientID' in the header. So I pass the following values in the header from postman.
{
"type":"TOKEN",
"authorizationToken": "testing2",
"methodArn": "arn:aws:execute-api:us-west-2:123456789012:ymy8tbxw7b/*/GET/",
"clientID" : "1000"
}
In this case, the Lambda function does not get the clientID. I checked various threads in SO, and understood that this can be achieved mapping header. So I did the following.
In the "Method Execution" section of the API method, I created a new header clientID. In the "Integration Request" section, under "HTTP Headers" section I provided the following value
Name: clientID
Mapped from: method.request.header.clientID
After doing the above, I deployed the API and tried to call the method from Postman, but the clientID is shown undefined. Following is the code that I have written in Lambda Function
exports.handler = function(event, context, callback) {
var clientid = event.clientID;
//I always get event.clientID undefined
console.log("The client ID is:" + event.clientID);
}
EDIT
Following is the error from the CloudWatch Log.
START RequestId: 274c6574-dea5-4009-b777-a929f84b9a9d Version: $LATEST
2019-09-19T09:40:25.944Z 274c6574-dea5-4009-b777-a929f84b9a9d INFO The client ID is:undefined
2019-09-19T09:40:25.968Z 274c6574-dea5-4009-b777-a929f84b9a9d ERROR Invoke Error
{
"errorType": "Error",
"errorMessage": "Unauthorized",
"stack": [
"Error: Unauthorized",
" at _homogeneousError (/var/runtime/CallbackContext.js:13:12)",
" at postError (/var/runtime/CallbackContext.js:30:51)",
" at callback (/var/runtime/CallbackContext.js:42:7)",
" at /var/runtime/CallbackContext.js:105:16",
" at Runtime.exports.handler (/var/task/index.js:40:4)",
" at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)",
" at process._tickCallback (internal/process/next_tick.js:68:7)"
]
}
I have understood why I was not getting the value in the header. I have done the following
1) Instead of type TOKEN, I used type REQUEST in the header. I understood this by reading the following link. This link also contains code for Request type.
https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html
2) I removed all the mapping from Method Request and Integration Request.
3) Deployed the API.

Magento 2.1.7: Unable to process binding "attr: function (){return {id:'billing-address-same-as-shipping-'+ getCode($parent)} }"

I get an error on my checkout payment page:
knockout.js:3012 Uncaught ReferenceError: Unable to process binding "attr: function (){return {id:'billing-address-same-as-shipping-'+ getCode($parent)} }"
Message: getCode is not defined
at attr (eval at createBindingsStringEvaluator (knockout.js:2624), <anonymous>:3:158)
at update (knockout.js:3659)
at ko.dependentObservable.disposeWhenNodeIsRemoved (knockout.js:3004)
at evaluateImmediate (knockout.js:1737)
at Object.ko.computed.ko.dependentObservable (knockout.js:1946)
at knockout.js:3002
at Object.arrayForEach (knockout.js:151)
at applyBindingsToNodeInternal (knockout.js:2974)
at applyBindingsToNodeAndDescendantsInternal (knockout.js:2854)
at applyBindingsToDescendantsInternal (knockout.js:2836)
Does somebody knows how to get this fixed?

Errors in codeigniter-restserver library

I want to use restful in my ci 3.03 application:
I found this tutplus tutorial
I downloaded codeigniter-restserver-master.zip file and copied Format.php and REST_Controller.php(#version 3.0.0) files into /application/libraries/REST directory
I created control application/controllers/api/Users.php :
require_once("application/libraries/REST/REST_Controller.php");
require_once("application/libraries/REST/Format.php");
class Users extends REST_Controller
{
//protected $rest_format = 'json';
function users_get()
{
//$users = $this->user_model->get_all();
$filter_username= $this->get('filter_username');
$filter_user_group= $this->get('filter_user_group');
$filter_active= $this->get('filter_active');
$sort= $this->get('sort');
$sort_direction= $this->get('sort_direction');
//, $filter_user_group, $filter_active, $sort, $sort_direction
$users_list = $this->muser->getUsersList(false, ''/*, $filter_username, $filter_user_group, $filter_active, $sort, $sort_direction, ''*/);
echo '<pre>'.count($users_list).'::$users_lists::'.print_r($users_list,true).'</pre>';
if($users_list)
{
$this->response($users, 200);
}
else
{
$this->response(NULL, 404);
}
}
AND RUNNING URL http://local-ci3.com/api/users I got many errors:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Users::$format
Filename: REST/REST_Controller.php
Line Number: 734
Backtrace:
File: /mnt/diskD_Work/wwwroot/ci3/application/libraries/REST/REST_Controller.php
Line: 734
Function: _error_handler
File: /mnt/diskD_Work/wwwroot/ci3/application/libraries/REST/REST_Controller.php
Line: 649
Function: response
File: /mnt/diskD_Work/wwwroot/ci3/index.php
Line: 292
Function: require_once
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Users::$format
Filename: REST/REST_Controller.php
Line Number: 752
Backtrace:
File: /mnt/diskD_Work/wwwroot/ci3/application/libraries/REST/REST_Controller.php
Line: 752
Function: _error_handler
File: /mnt/diskD_Work/wwwroot/ci3/application/libraries/REST/REST_Controller.php
Line: 649
Function: response
File: /mnt/diskD_Work/wwwroot/ci3/index.php
Line: 292
Function: require_once
Actually I wanted to get some workable library to help me with REST api creation. I think that is preferable way istead of making from zero.
But is this library not workable or does it needs for some fixing? Sorry, what I missed is if this library only for ci 2?
I made search on this forum and found such hint :
I have the same problem when I load both Format.php and
Rest_Controller.php into a controller. After have a quick glance at
Format.php, it appears to be a standalone format conversion helper.
Try to just load Rest_Controller.php and see if your problem goes
away.
I commented line
//require_once("application/libraries/REST/Format.php");
in my controller, but I still get errors like :
Message: Undefined property: Users::$format.
I tried to review code of this library and see that invalid block when data are converted to json format, line 731-757 :
elseif ($data !== NULL)
{
// If the format method exists, call and return the output in that format
if (method_exists($this->format, 'to_' . $this->response->format))
{
// Set the format header
$this->output->set_content_type($this->_supported_formats[$this->response->format], strtolower($this->config->item('charset')));
$output = $this->format->factory($data)->{'to_' . $this->response->format}();
// An array must be parsed as a string, so as not to cause an array to string error
// Json is the most appropriate form for such a datatype
if ($this->response->format === 'array')
{
$output = $this->format->factory($output)->{'to_json'}();
}
}
else
{
// If an array or object, then parse as a json, so as to be a 'string'
if (is_array($data) || is_object($data))
{
$data = $this->format->factory($data)->{'to_json'}();
}
// Format is not supported, so output the raw data as a string
$output = $data;
}
}
If I tried to commented this block, but get error
Message: Array to string conversion
Looks like data are not converted in this case...
Is is possible to fix these errors?
Or can you, please, to tell me advice some codeigniter 3 REST api workable library with similar interface like library above?
Thanks!
I use that lib, work just fine. My suggestion is follow the more relevant installation instruction on github .
you also wrong place the lib file :
Tutorial say :
require(APPPATH'.libraries/REST_Controller.php');
You try :
require_once("application/libraries/REST/REST_Controller.php");
require_once("application/libraries/REST/Format.php");
No need to include the format because on line 407 the lib will load it. And also good to know on line 404 it will load the configuration (application/config/rest.php) it will be your default configuration, and also you can change it to suit your need.
Please let me know if you still got error using my answer :)

flickr.auth.getFrob by tcl (REST)

I'm trying to get response from Flickr by using Flickr API but I have no idea and no examples in TCL for that.
I wrote the following code:
#!/usr/bin/tclsh
package require rest
set flickr(auth.getFrob) {
url http://api.flickr.com/services/rest/
req_args { api_key: }
}
rest::create_interface flickr
puts [flickr::auth::getFrob -api_key ea4a4134e2821898e5e31713d2ad74fd ]
When I execute it I get this error:
invalid command name "flickr::auth::getFrob"
while executing
"flickr::auth::getFrob -api_key ea4a4134e2821898e5e31713d2ad74fd "
invoked from within
"puts [flickr::auth::getFrob -api_key ea4a4134e2821898e5e31713d2ad74fd ]"
(file "./flickr.tcl" line 17)
=====================================
I've updated the last line of the code as proposed by Johannes to:
puts [flickr::auth.getFrob -api_key ea4a4134e2821898e5e31713d2ad74fd ]
but still got the strange response:
rsp {stat fail} {{err {code 112 msg {Method "unknown" not found}} {}}}
when supposed something like:
<frob>746563215463214621</frob>
as described in Flickr API help:
auth.getFrob
It looks like the command name is
::flickr::auth.getFrob

facebook api in R: OAuthException error, code 803

Im trying to connect to Facebook using R. I tried to use the codes from
https://github.com/datamgmt/facebook-data-mining
Which gets the user information with the following code:
access.token<- "AAACE________n6bcITRIMCZCKhMySOV" #my access token
FacebookFetch <- function(access.path = "me", access.token, access.options) {
if (missing(access.options)) {
access.options <- ""
}
else {
access.options <- sprintf("?%s", paste(names(access.options), "=",
unlist(access.options), collapse = "&", sep = ""))
}
data <- getURL(sprintf("https://graph.facebook.com/%s%s&access_token=%s",
access.path, access.options, access.token),cainfo="cacert.pem")
fromJSON(data)
}
individual.id <- "100__72" # my ID
individual <- FacebookFetch(access.path=paste(individual.id),access.token=access.token )
if (length(individual$id)) {
cat ("Working with individual: ",individual$name," (",individual$id,")\n", sep="")
} else{
cat("Message: ", unlist(individual)[1], "\n")
cat("Type: ", unlist(individual)[2], "\n")
cat("Code: ", unlist(individual)[3], "\n")
stop(" Cannot continue")
}
where cainfo="cacert.pem" is added to work around the SSL certificate verification error.
Although now I get the following error and I have no idea how to solve this.
Message: (#803) Some of the aliases you requested do not exist: 100__72&access_token=AAACE________n6bcITRIMCZCKhMySOV
Type: OAuthException
Code: 803
I found some posts where people got the same problem, only not using R but other programs, the problem was fixed by making the token and id a character. However, in R, these are already of class character.
When im recreating the url in the browser it returns the same error, while the graph explorer returns all the correct information. I have tried to refresh the token multiple times, but that didn't work.
The API reference book doesn't state what error code 803 entails.
does anybody have an idea how to solve this?
Im using r 2.15.2 in Rstudio on a windows 7 pro (also tried it on windows vista)