我正在使用Sendgrid和NodeJS发送电子邮件,但收到以下错误:
D:\NodejsCODE\email_app\app.js:19
from_email = new helper.Email("test@example.com")
^
TypeError: Cannot read property 'Email' of undefined
at Object.<anonymous> (D:\NodejsCODE\email_app\app.js:19:24)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
代码:
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var config = require('./config');
var index = require('./routes/index');
var users = require('./routes/users');
var app = express();
app.set('my_api_key', config.access_key);
var helper = require('sendgrid').mail
from_email = new helper.Email("test@example.com")
to_email = new helper.Email("rahilansari261@gmail.com")
subject = "Sending with SendGrid is Fun"
content = new helper.Content("text/plain", "and easy to do anywhere, even with Node.js")
mail = new helper.Mail(from_email, subject, to_email, content)
var sg = require('sendgrid')(my_api_key);
var request = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: mail.toJSON()
});
sg.API(request, function(error, response) {
console.log(response.statusCode)
console.log(response.body)
console.log(response.headers)
})
// view engine setup
app.set('views', path.join(__dirname, 'views'));
var port = process.env.PORT || 3000;
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// console.log("hlo");
app.use('/', index);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
app.listen(port);
console.log('Magic Happens @ http://localhost:' + port);
module.exports = app;
答案 0 :(得分:0)
使用以下代码,似乎工作正常。你说的那条线没有错误。您的设置可能存在问题吗?
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var app = express();
app.set('my_api_key', 'key');
var helper = require('sendgrid').mail
from_email = new helper.Email("test@example.com")
to_email = new helper.Email("rahilansari261@gmail.com")
subject = "Sending with SendGrid is Fun"
content = new helper.Content("text/plain", "and easy to do anywhere, even with Node.js")
mail = new helper.Mail(from_email, subject, to_email, content)
var sg = require('sendgrid')(my_api_key);
var request = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: mail.toJSON()
});
sg.API(request, function(error, response) {
console.log(response.statusCode)
console.log(response.body)
console.log(response.headers)
})
// view engine setup
app.set('views', path.join(__dirname, 'views'));
var port = process.env.PORT || 3000;
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// console.log("hlo");
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
app.listen(port);
console.log('Magic Happens @ http://localhost:' + port);
module.exports = app;