javascript 结果:ReferenceError:未定义响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32058315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 14:47:21  来源:igfitidea点击:

Result: ReferenceError: response is not defined

javascriptpostparse-platformsendgridcloud-code

提问by csakon

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 coming through. However, when I look at my logs, I get the following:

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

Result: ReferenceError: response is not defined

//main.js

//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");
    }
  });
});

回答by Johannes Jander

responseis not defined, I think you meant to write

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

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.

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