0

I have a post API request that I need help with. I would like to call a function after the Post request, but the function isn't being executed.

var restful = require('node-restful');
module.exports = function(app, route) {
  // Setup the controller for REST.
  var rest = restful.model(
    'post',
    app.models.post
  ).methods(['get', 'put', 'post', 'delete']);

  // Register this endpoint with the application.
  rest.register(app, route);
  rest.after('post', send_me_email)

  function send_me_email(req, res, next) {
    server.send({
     text:    "i hope this works", 
     from:    "****@******.com", 
     to:      "******@gmail.com",
     subject: "testing emailjs"
    }, 
    function(err, message) { 
      console.log(err || message); 
    })

    console.log("HERE IS THE POST AFTER: " + req.body);
    next();
  }

  // Return middleware.
  return function(req, res, next) {
    next();
  };
}
0

1 Answer 1

1
    function send_me_email(req, res, next) {
        server.send({
         text:    "i hope this works", 
         from:    "****@******.com", 
         to:      "******@gmail.com",
         subject: "testing emailjs"
        }, 
        function(err, message) { 
          console.log(err || message); 
        })

        console.log("HERE IS THE POST AFTER: " + req.body);
        next();
      }

should be

function send_me_email(req, res, next) {
    server.send({
     text:    "i hope this works", 
     from:    "****@******.com", 
     to:      "******@gmail.com",
     subject: "testing emailjs"
    }, 
    function(err, message) { 
      console.log(err || message);

       if(err) return next(err);

       console.log("HERE IS THE POST AFTER: " + req.body);
       next();

    })

  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.