mailjet / mailjet-apiv3-nodejs

[API v3] Official Mailjet API v3 NodeJS wrapper
https://dev.mailjet.com
MIT License
236 stars 69 forks source link

Unexpected token { #169

Closed joseadame closed 2 years ago

joseadame commented 3 years ago

Hi! When a try to deploy a Firebase function that uses node-mailjet with node 12 it throws de following error:

SyntaxError: Unexpected token {
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:617:28)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)

I have the following versions in package.json

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint .",
    "serve": "firebase serve --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "10"
  },
  "dependencies": {
    "firebase-admin": "^9.2.0",
    "firebase-functions": "^3.11.0",
    "node-mailjet": "^3.3.1"
  },
  "devDependencies": {
    "eslint": "^5.12.0",
    "eslint-plugin-promise": "^4.0.1",
    "firebase-functions-test": "^0.1.6"
  },
  "private": true
}
scroll17 commented 2 years ago

Hello @joseadame

I tried to reproduce your error but I didn't receive the same and deployed functions that used node-mailjet@3.1.1 without any errors.

Below I provided my source files and the result of deploying and requests to Firebase Functions:


My package.json:

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint .",
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "12"
  },
  "main": "index.js",
  "dependencies": {
    "firebase-admin": "^10.0.2",
    "firebase-functions": "^3.18.0",
    "node-mailjet": "^3.3.1"
  },
  "devDependencies": {
    "eslint": "^8.9.0",
    "eslint-config-google": "^0.14.0",
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}

My index.js:

const functions = require("firebase-functions");
const Mailjet = require("node-mailjet");

exports.helloWorld = functions.https.onRequest((request, response) => {
  functions.logger.info("Hello logs!", {structuredData: true});
  response.send("Hello from Firebase!");
});

exports.testMailjet = functions.https.onRequest((request, response) => {
  const mailjet = Mailjet
      .connect(
          "public_key",
          "private_key"
      );

  const fromEmail = "pilot@mailjet.com";        // here your email 
  const toEmail = "passenger@mailjet.com";  // here your email 

  const emailRequest = mailjet
      .post("send")
      .request({
        "FromEmail": fromEmail,
        "FromName": "Pilot",
        "Subject": "Hello world from Firebase and Mailjet",
        "Text-part": "Hello World",
        "Recipients": [
          {
            Email: toEmail,
          },
        ],
      });

  functions.logger.debug("Start send email", {structuredData: true});
  emailRequest
      .then((result) => {
        functions.logger.info("Email sent!", {structuredData: true});
        response.status(201).send(result);
      })
      .catch((err) => {
        functions.logger.error("Email not send", {structuredData: true});
        response.status(500).send(err);
      });
});

This is my deploy logs: image


Response for helloWorld endpoint:

Hello from Firebase!

Response for testMailjet endpoint:

{
  "response": {
    "req": {
      "method": "POST",
      "url": "https://api.mailjet.com/v3/send",
      "data": {
        "FromEmail": "pilot@mailjet.com",
        "FromName": "Pilot",
        "Subject": "Hello world from Firebase and Mailjet",
        "Text-part": "Hello World",
        "Recipients": [
          {
            "Email": "passenger@mailjet.com"
          }
        ]
      },
      "headers": {
        "user-agent": "mailjet-api-v3-nodejs/3.3.1",
        "content-type": "application/json",
        "authorization": "Basic OWQ0YWVkOTU0ZGI5YTY2YTkxNDAxMjJhYTI4Tk3OWM1MTdkYTFkYmE1NmE0NTY0YjUyMDhiN2M="
      }
    },
    "header": {
      "date": "Mon, 25 Jul 2022 12:52:32 GMT",
      "content-type": "application/json",
      "content-length": "151",
      "connection": "close"
    },
    "status": 200,
    "text": "{ \"Sent\" : [{ \"Email\" : \"passenger@mailjet.com\", \"MessageID\" : 1152921517897215880, \"MessageUUID\" : \"3aab11a5-37cb-4099-86c0-439bbf081cce\" }] }\n"
  },
  "body": {
    "Sent": [
      {
        "Email": "passenger@mailjet.com",
        "MessageID": "1152921517897215880",
        "MessageUUID": "3aab11a5-37cb-4099-86c0-439bbf081cce"
      }
    ]
  }
}

We added the Firebase Functions example-app for you and other users who could want to use node-mailjet with Firebase Functions.

In this example-app we used: