Faris
Faris

Reputation: 1236

no such file or directory in Parse server - cloud code

I'm using the Parse server and I'm trying to send an email with an Html file. the problem I'm not really sure how to access the public folder from the cloud code.

This is the error:

ENOENT: no such file or directory, open './public/invoice.html'

The directory:

│  ├── cloud
│  │   ├── functions.js
│  │   ├── main.js
│  ├── public
│  │   ├── invoice.html

fs.readFileSync("../public/invoice.html", "utf8"

And this is my code:

  var fs = require('fs');

  Parse.Cloud.define("mailSend", function(request, response) {
    const apiKey = '4441*****************a47f';
    const mailgun = require("mailgun-js");
    const DOMAIN = 'user.mailgun.org';
    const mg = mailgun({apiKey: apiKey, domain: DOMAIN});
    const data = {
      from: 'email <[email protected]>',
      to: '[email protected]',
      subject: 'Invoice',
      html:  fs.readFileSync("../public/invoice.html", "utf8") ||  null
    };
    mg.messages().send(data, function (error, body) {
      console.log(body);
    });
});

Upvotes: 0

Views: 353

Answers (3)

Faris
Faris

Reputation: 1236

Thank you all for your answers, This code works fine with me. I used ejs instead of HTML. you can use HTML but you have to add

var fs = require('fs'); insteead of const ejs = require('ejs');

var path = require('path');
const ejs = require('ejs');
const mailgun = require("mailgun-js");
const apiKey = '444**********00a47f';
const DOMAIN = 'user.mailgun.org';
const mg = mailgun({apiKey: apiKey, domain: DOMAIN});

Parse.Cloud.define("SendEmail", function(request, response) {
        var orderId = request.params.orderId;
        var items = request.params.items;
        var details = request.params.details;
        var user = request.params.user;
        var subject = "Thank You for Your Order #" + orderId;
        var orderData = {
          user: user,
          items: items,
          details: details
        }

        ejs.renderFile(path.join(__dirname, './public/invoice.ejs'), orderData, (err, htmlString) => {
          if (err) console.error(err);

          let data = {
            from: 'app neme <[email protected] >',
            to: '[email protected]',
            subject: subject,
            html: htmlString
          };

          mg.messages().send(data, function (error, body) {
            console.log(body);
            console.log(prods);
            response.success("success send");
          });
        });

    });

Upvotes: 0

Davi Mac&#234;do
Davi Mac&#234;do

Reputation: 2984

Try this:

var path = require('path');
var fs = require('fs');

  Parse.Cloud.define("mailSend", function(request, response) {
    const apiKey = '4441*****************a47f';
    const mailgun = require("mailgun-js");
    const DOMAIN = 'user.mailgun.org';
    const mg = mailgun({apiKey: apiKey, domain: DOMAIN});
    const data = {
      from: 'email <[email protected]>',
      to: '[email protected]',
      subject: 'Invoice',
      html:  fs.readFileSync(path.join(__dirname, "../public/invoice.html"), "utf8") ||  null
    };
    mg.messages().send(data, function (error, body) {
      console.log(body);
    });
});

Upvotes: 1

shubhambharti201
shubhambharti201

Reputation: 380

Looks similar. Check this and try, Error: ENOENT: no such file or directory, unlink

Upvotes: 0

Related Questions