最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Result: ReferenceError: response is not defined - Stack Overflow

programmeradmin3浏览0评论

I'm trying to send an email via a form with SendGrid + Parse Cloud Code Modules. Everything is working fine functionally and the emails are ing through. However, when I look at my logs, I get the following:

Result: ReferenceError: response is not defined

//main.js

var express = require('express');
var sendgrid = require('sendgrid');
sendgrid.initialize("USER", "PASS");

var app = express();

// App configuration section
app.use(express.bodyParser());    // Middleware for reading request body


app.post('/email', function(req, res) {
  var name = req.body.name;
  var email = req.body.email;
  var message = req.body.message;

  sendgrid.sendEmail({
    to: '[email protected]',
    from: email,
    subject: 'Message from ' + name + ' via Web',
    text: message
  }, {
    success: function(httpResponse) {
      console.log(httpResponse);
      response.success("Email sent!");
    },
    error: function(httpResponse) {
      console.error(httpResponse);
      response.error("Uh oh, something went wrong");
    }
  });
});

I'm trying to send an email via a form with SendGrid + Parse Cloud Code Modules. Everything is working fine functionally and the emails are ing through. However, when I look at my logs, I get the following:

Result: ReferenceError: response is not defined

//main.js

var express = require('express');
var sendgrid = require('sendgrid');
sendgrid.initialize("USER", "PASS");

var app = express();

// App configuration section
app.use(express.bodyParser());    // Middleware for reading request body


app.post('/email', function(req, res) {
  var name = req.body.name;
  var email = req.body.email;
  var message = req.body.message;

  sendgrid.sendEmail({
    to: '[email protected]',
    from: email,
    subject: 'Message from ' + name + ' via Web',
    text: message
  }, {
    success: function(httpResponse) {
      console.log(httpResponse);
      response.success("Email sent!");
    },
    error: function(httpResponse) {
      console.error(httpResponse);
      response.error("Uh oh, something went wrong");
    }
  });
});
Share Improve this question asked Aug 17, 2015 at 19:35 csakoncsakon 6112 gold badges7 silver badges25 bronze badges 5
  • 1 The error is pretty clear to me. You never defined the response variable you call. Didn't you mean res ? Or console ? Don't forget to fix that both in your success handler and your error handler. – blex Commented Aug 17, 2015 at 19:37
  • That's what I figured, but when I do that I get the following: Result: TypeError: Object [object Object] has no method 'success' – csakon Commented Aug 17, 2015 at 19:43
  • Where do you want to log these messages? console.log is not what you want? I can't find the doc for the sendgrid.sendEmail function :/ – blex Commented Aug 17, 2015 at 19:49
  • @blex - yeah, unfortunately parse isn't fantastic at providing docs for some of this stuff. Here is where I got this from - github./elbuo8/sendgrid-parse – csakon Commented Aug 17, 2015 at 21:04
  • Ok, then I think they made a mistake. You should post an issue here, I think it's the best thing you can do to get a proper answer. – blex Commented Aug 17, 2015 at 21:38
Add a ment  | 

1 Answer 1

Reset to default 1

response is not defined, I think you meant to write

sendgrid.sendEmail({
    to: '[email protected]',
    from: email,
    subject: 'Message from ' + name + ' via Web',
    text: message
}, {
    success: function(httpResponse) {
      console.log(httpResponse);
      httpResponse.success("Email sent!");
    },
    error: function(httpResponse) {
      console.error(httpResponse);
      httpResponse.error("Uh oh, something went wrong");
    }
});

BTW: you should always set strict mode via "use strict" - that would catch undefined variables even faster.

发布评论

评论列表(0)

  1. 暂无评论