无论我在哪里使用,我都会复制一个配置变量。我想让它在一种方法中通用,并在整个项目中使用该方法的变量。
common.ts
Notification(){
User.findOne({ email: data.email }, (err, user) => {
if (err) {
console.log(err);
}
else {
if (user.device_info[0].platform == 'Android') {
var Notification_config = new SNS({
platform: SNS.SUPPORTED_PLATFORMS.ANDROID,
accessKeyId: cache.get('AMAZON_ACCESS_KEY'),
secretAccessKey: cache.get('AMAZON_SECRET_KEY'),
region: cache.get('AMAZON_REGION'),
platformApplicationArn: 'arn:aws:sns:us-west-1:XXXXXXXX:app/GCM/Test',
});
console.log("Andriod");
}
else {
console.log("iOS");
var Notification_config = new SNS({
platform: SNS.SUPPORTED_PLATFORMS.IOS,
accessKeyId: cache.get('AMAZON_ACCESS_KEY'),
secretAccessKey: cache.get('AMAZON_SECRET_KEY'),
region: cache.get('AMAZON_REGION'),
platformApplicationArn: 'arn:aws:sns:us-west-1:XXXXXX:app/APNS_SANDBOX/Test',
sandbox: true
});
}
})
}
abc.ts
Notification_config.addUser('dev_token', JSON.stringify({
some: 'extra data'
}), function (err, endpointArn) {
if (err) {
throw err;
}
// Send a simple String or data to the client
Notification_config.sendMessage(endpointArn, ` sent you a request`, function (err, messageId) {
if (err) {
res.send(err)
}
req; res;
console.log('Message sent, ID was: ' + messageId);
});
我尝试过的
function push_notification(email) {
User.findOne({
email: email
}, (err, data) => {
if (err) {
console.log(err)
} else {
if (data.device_info[0].platform == 'Android') {
var platform = SNS.SUPPORTED_PLATFORMS.ANDROID;
var arn = 'arn:aws:sns:us-east-1:XXXXXX:app/GCM/Test'
//console.log("Andriod");
} else {
var platform = SNS.SUPPORTED_PLATFORMS.IOS;
var arn = 'arn:aws:sns:us-east-1:XXXXX:app/APNS_SANDBOX/Test'
//console.log("iOS");
}
var Notification_config = new SNS({
platform: platform,
accessKeyId: cache.get('AMAZON_ACCESS_KEY'),
secretAccessKey: cache.get('AMAZON_SECRET_KEY'),
region: cache.get('AMAZON_REGION'),
platformApplicationArn: arn,
sandbox: true
});
}
return Notification_config;
});
}
export default push_notification;
我想在 abc.ts
中使用 common.ts
中的变量 Notification_config
。我试图将 Notification_config
返回到方法,并在 common.ts 中将 export default Notification
导入到 abc.ts 中,但找不到变量。谁能帮帮我。
最佳答案
Use Promises
像这样:
common.ts
export default function Notification(data:any){
return new Promise(function (resolve, reject) {
User.findOne({ email: data.email }, (err, user) => {
if (err) {
console.log(err);
reject(err)
}
else {
if (user.device_info[0].platform == 'Android') {
var Notification_config = new SNS({
platform: SNS.SUPPORTED_PLATFORMS.ANDROID,
accessKeyId: cache.get('AMAZON_ACCESS_KEY'),
secretAccessKey: cache.get('AMAZON_SECRET_KEY'),
region: cache.get('AMAZON_REGION'),
platformApplicationArn: 'arn:aws:sns:us-west-1:XXXXXXXX:app/GCM/Test',
});
console.log("Andriod");
resolve(Notification_config);
}
else {
console.log("iOS");
var Notification_config = new SNS({
platform: SNS.SUPPORTED_PLATFORMS.IOS,
accessKeyId: cache.get('AMAZON_ACCESS_KEY'),
secretAccessKey: cache.get('AMAZON_SECRET_KEY'),
region: cache.get('AMAZON_REGION'),
platformApplicationArn: 'arn:aws:sns:us-west-1:XXXXXX:app/APNS_SANDBOX/Test',
sandbox: true
});
resolve(Notification_config);
}
})
})
}
abc.ts
import Notification from './common';
Notification(data).then(function (Notification_config) {
// Now you have access to 'Notification_config'
})
关于javascript - 使用从一种方法到另一种方法的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57917614/