我正在尝试从(我猜) transporter.sendMail的内置返回值中获取ip地址,因为我发现内置返回值中包含ip地址。我必须以这种方式获取ip,因为正常的var ip = req.clientIp;方式只输出::1。
我花了很多时间来解决这个问题,但我做不到。然后,我在堆栈溢出上阅读了很多关于闭包的帖子,因为我的问题与此类似,但经过几天的阅读和尝试,我发现它根本就不一样。有些解决方案只能在纯闭包的情况下工作,但是当我把它移到我的用例/代码中时,它根本不起作用。
问题是信息只存在于它的作用域中(或者只存在于回调函数中),我可以注销信息数据,但我不能将其作为变量传递出去,因此我可以在该作用域之外使用它。如果你试图在作用域之外输出信息,那么带有info数据的变量将是未定义的。
这是因为node.js将首先运行除函数之外的所有其他代码,因此如果函数没有首先读取,那么即使我将调用变量的代码放在函数之后,变量仍将是未定义的,因为信息不存在于函数之外。即使我试图在调用变量之前调用函数,并将信息传递出去,我也不能传递变量,或者至少在我调用变量之前。
router.get('/', function(req, res, next) {
var transporter = nodemailer.createTransport({
service: 'Gmail',
//var info=info.response,
auth: {
user: 'xxxxx', // Your email id
pass: 'xxxxx' // Your password
}//auth
});//transporter=nodemailer.create
var mailOptions={
to: 'xxxxxx',
subject: req.body,
//subject: "lt",
text: "Email Test Has Passed"
}//mailOptions
transporter.sendMail(mailOptions, function (error, info, callback){
if(error){
console.log(error);
res.json({yo: 'error'});
}else{
console.log('Message sent: ' + info.response);
//console.log('info sent: ' + info);//[obj obj]
infor=info.response;
res.json({yo: info.response});
};//else
});//();//transporter.sendmail(mailoptions)
res.render('index', {
title: 'laundrytake: we clean and deliver your laundry/dry cleaning',
page:'Home',
});//atw is a local var inside the temp index feeded from myArtwork
});//router.get(/)这是我尝试过的方法。我喜欢在评论中记录我所做的事情。
router.get('/', function(req, res, next) {
var infor;
var func;
//var func={};
// var p=req.ip;
// inside middleware handler
// var ipMiddleware = function(req, res, next) {
// var clientIp = requestIp.getClientIp(req);
// next();
// };
// ipMiddleware;
// on localhost you'll see 127.0.0.1 if you're using IPv4
// or ::1, ::ffff:127.0.0.1 if you're using IPv6
// app.use(requestIp.mw())
// app.use(function(req, res) {
// var ip = req.clientIp;
// res.end(ip);
// });
// var ip;
// if (req.headers['x-forwarded-for']) {
// ip = req.headers['x-forwarded-for'].split(",")[0];
// } else if (req.connection && req.connection.remoteAddress) {
// ip = req.connection.remoteAddress;
// } else {
// ip = req.ip;
// }console.log("client IP is *********************" + ip);
// var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
// if (ip.substr(0, 7) == "::ffff:") {
// ip = ip.substr(7)
// }
// console.log("client IP is:");
// console.log(ip);
var transporter = nodemailer.createTransport({
service: 'Gmail',
//var info=info.response,
auth: {
user: 'xxxxx', // Your email id
pass: 'xxxxx' // Your password
}//auth
});//transporter=nodemailer.create
// console.log("xxx");
//var text = 'Hello world from \n\n' + req.body.name;
var mailOptions={
to: 'xxxxxx',
subject: req.body,
//subject: "lt",
text: "Email Test Has Passed"
}//mailOptions
transporter.sendMail(mailOptions, function (error, info, callback){
if(error){
console.log(error);
res.json({yo: 'error'});
}else{
console.log('Message sent: ' + info.response);
//console.log('info sent: ' + info);//[obj obj]
infor=info.response;
res.json({yo: info.response});
// app.locals.infor=info.response;
//return(infor, //log infor dnt fix
//console.log("ff"));//ff printout in the end
//callback(infor);//log infor dnt fix //log r [f sendmail]
//return function(){return infor};//log r [f sendmail]. with info involk })(info);info undef, with 3 or () involk tran.sendM(...)not a f
//sign(info.response);//no print out
// return (
// function(){
// //infor=info.response;
// console.log("out :");
// }
// )//}
// func=(function() { //create a new scope using a wrapper function
// var ilocal = info.response; //capture i into a local var
// return function() { //return the inner function
// console.log("My value: " + ilocal);
// };//return f()
// })();//f()
};//else
});//();//transporter.sendmail(mailoptions)
//function sign (ff){var xx=ff; console.log(ff);}
// ino=transporter.sendMail();
// ino.out;
//ff //ff() //ff not def return(func ff(){var xx=info.res})
var sign = function( infor ) {//no callback unhandled promise rejec warning no recipients def err
console.log("User: " + infor + " is saved successfully.")
};
var r =transporter.sendMail;//infor: [f: sendMail] undef
//transporter.sendMail( sign );//callback(infor) nothing inside sendMail warning
//transporter.sendMail(mailOptions, sign );//user: null is saved succ
console.log("infor :");
console.log(infor);
console.log(r);
//console.log(info.response);//info not def
//func();//not a f
res.render('index', {
title: 'laundrytake: we clean and deliver your laundry/dry cleaning',
page:'Home',
});//atw is a local var inside the temp index feeded from myArtwork
});//router.get(/)发布于 2018-06-07 23:15:33
$( window ).load(function() {
console.log("loaded");
$.ajax({
type: 'POST',//{ '"71.11.188.189"': '' }
data: userip,//JSON.stringify(userip),//userip,//ipad
url: '/yhu/fkip',//same err af remove sendMail from index.js /
dataType: 'JSON' //text same print save 500 err
}).done(function( response ) {
console.log("ajax POST ip");//never printed
console.log(userip);
if (response.msg === '') {//w/o this //POST /yhu/fkip 500(internal server err)
console.log("res.msg null");
}//if(response.msg)
else {
// If something goes wrong, alert the error message that our service returned
alert('Error: ' + response.msg);
}//else
});//.done(function(response
});//$(window).load(function
https://stackoverflow.com/questions/45827060
复制相似问题