【问题标题】:Result: ReferenceError: response is not defined结果:ReferenceError:未定义响应
【发布时间】:2015-08-17 19:35:34
【问题描述】:

我正在尝试通过带有 SendGrid + Parse Cloud Code Modules 的表单发送电子邮件。一切都在功能上正常工作,并且电子邮件正在通过。但是,当我查看日志时,我得到以下信息:

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@gmail.com',
    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");
    }
  });
});

【问题讨论】:

  • 这个错误对我来说很清楚。您从未定义过您调用的 response 变量。你不是说res 吗?还是console?不要忘记在 success 处理程序和 error 处理程序中修复它。
  • 这就是我的想法,但是当我这样做时,我得到以下信息: 结果:TypeError: Object [object Object] has no method 'success'
  • 您想在哪里记录这些消息? console.log 不是你想要的吗?我找不到sendgrid.sendEmail 函数的文档:/
  • @blex - 是的,不幸的是 parse 在为这些东西提供文档方面并不出色。这是我从这里得到的 - github.com/elbuo8/sendgrid-parse
  • 好的,那我认为他们犯了一个错误。您应该发布问题here,我认为这是获得正确答案的最佳方法。

标签: javascript post parse-platform sendgrid parse-cloud-code


【解决方案1】:

response 没有定义,我想你的意思是写

sendgrid.sendEmail({
    to: 'email@gmail.com',
    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");
    }
});

顺便说一句:您应该始终通过“使用严格”设置严格模式 - 这样可以更快地捕获未定义的变量。

【讨论】:

    猜你喜欢
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    • 2012-08-24
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    相关资源
    最近更新 更多