1

I am duplicating a configuration variable wherever I am Using. I want to make that common in one method and use that method's variable across the project.

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

What I tried

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;

I want the variable Notification_config from common.ts to be used in abc.ts. I have tried to return the Notification_config to method and export default Notification in common.ts and import it in abc.ts but It couldn't find the variable. Can someone help me out.

2
  • "I tried ..." please show that try. Commented Sep 13, 2019 at 5:18
  • Please check the updated one Commented Sep 13, 2019 at 5:36

1 Answer 1

1

Use Promises

Like so:

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'
})
Sign up to request clarification or add additional context in comments.

2 Comments

When I console i could get the value but I couldn't use Notification_config.addUser('dev_token', JSON.stringify({ in abc.ts I am getting Object is of type 'unknown'
With the old code just Notification(data).then(function (Notification_config:any) works!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.