【问题标题】:value of token are null between functions令牌的值在函数之间为空
【发布时间】:2017-04-22 09:30:28
【问题描述】:

我正在使用带有来自https://github.com/fechanique/cordova-plugin-fcm 的cordova-plugin-fcm 插件的ionic 2

我在将令牌值分配给 deviceId 时遇到问题,console.log 在调用中正确显示该值,但在外部返回 null

代码如下:

declare var FCMPlugin;

@Component({
  templateUrl: `app.template.html`
})
export class MyApp {
  @ViewChild(Nav) nav: Nav;
  public deviceId: any;


  appPages: PageObj[] = [
    { title: 'Home', component: HomePage, icon: 'home' },
    { title: 'Profile', component: UserProfilePage, icon: 'calendar' },
    { title: 'Give Feedback', component: FeedbackPage, icon: 'calendar' },
  ];

  rootPage: any;
  TutorialPage: TutorialPage;

  constructor(platform: Platform,
    public menu: MenuController,
    private actionSheetCtrl: ActionSheetController,
    public authService: AuthService,
    public dataService: DataService) {

    platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      StatusBar.styleDefault();
      Splashscreen.hide();

      FCMPlugin.getToken(
        function (token) {
          console.log('device token is ' + token);
           var deviceId = token;
          console.log('value of id is' + deviceId);
        },
        function (err) {
          console.log('error retrieving token: ' + err);
        }
      );
    });
  }

  ngOnInit() {
    var self = this;
    this.authService.onAuthStateChanged(function (user) {
      if (user === null) {
        self.nav.setRoot(TutorialPage);
      }
    });
  }

  ngAfterViewInit() {
        var self = this;
    this.authService.onAuthStateChanged(function (user) {
      if (user === null) {
        self.menu.close();
        self.nav.setRoot(TutorialPage);
      } else {
        console.log("device token upon login is" + this.deviceId);
        let uid = self.authService.getLoggedInUser().uid;
        console.log("uid at login is " + uid);
        self.dataService.setDeviceToken(uid, this.deviceId);
      }
    });

  }

deviceId 的值在 ngAfterViewInit 中显示为 null

终于开始工作了:----

有效的编辑代码是:

declare var FCMPlugin;

@Component({
  templateUrl: `app.template.html`
})
export class MyApp {
  @ViewChild(Nav) nav: Nav;
  public deviceId: any;


  appPages: PageObj[] = [
    { title: 'Home', component: HomePage, icon: 'home' },
    { title: 'Profile', component: UserProfilePage, icon: 'calendar' },
    { title: 'Give Feedback', component: FeedbackPage, icon: 'calendar' },
  ];

  rootPage: any;
  TutorialPage: TutorialPage;

  constructor(platform: Platform,
    public menu: MenuController,
    private actionSheetCtrl: ActionSheetController,
    public authService: AuthService,
    public dataService: DataService) {

    platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      StatusBar.styleDefault();
      Splashscreen.hide();

      FCMPlugin.getToken(
        token => {
          var self = this;
          console.log('device token is ' + token);
          let devicetoken = token;
          console.log('value of id is' + devicetoken);
          self.deviceId = devicetoken;

        },
        function (err) {
          console.log('error retrieving token: ' + err);
        }
      );
    });
  }

  ngOnInit() {
    var self = this;
    this.authService.onAuthStateChanged(function (user) {
      if (user === null) {
        self.nav.setRoot(TutorialPage);
      }
    });
  }

  ngAfterViewInit() {
    var self = this;
    this.authService.onAuthStateChanged(function (user) {
      if (user === null) {
        self.menu.close();
        self.nav.setRoot(TutorialPage);
      } else {
        console.log("device token upon login is" + self.deviceId);
        let uid = self.authService.getLoggedInUser().uid;
        console.log("uid at login is " + uid);
        self.dataService.setDeviceToken(uid, self.deviceId);
      }
    });

  }

【问题讨论】:

    标签: javascript cordova push-notification ionic2 cordova-plugins


    【解决方案1】:

    第一次使用'deviceId'变量是在函数中声明的局部变量。

    “deviceId”的第二个用途是 MyApp 实例(对象)中的一个字段,并且从未分配过。

    改为:

    FCMPlugin.getToken(
        token => {
          console.log('device token is ' + token);
           var deviceId = token;
          console.log('value of id is' + deviceId);
           this.deviceId = token;
        },
        function (err) {
          console.log('error retrieving token: ' + err);
        }
      );
    

    还要确保从实例中检索到 deviceId,因此在回调函数 onAuthStateChanged 中,使用 self.deviceId 而不是 this.deviceId

    【讨论】:

    • 不显示它的不工作错误是,它显示 this.deviceId 未定义,setdevice 令牌处的 uid 和设备令牌是 NUBeUq2l7SPNA7xJ7xd7tLKexmB3 > 未定义
    • 它应该在“登录时的设备令牌未定义”处显示令牌
    • 哦,是的,我认为问题是你在函数中使用了“this”......所以再次修复使用你保留的 self(例如用 self.deviceId 替换 this.deviceId)
    • 非常感谢,我正在更新问题中的工作代码:),您能否提供指向某种文档的链接,我可以在其中了解我所缺少的内容......我想这与箭头函数有关。
    猜你喜欢
    • 2016-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-13
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    相关资源
    最近更新 更多