首页 > 解决方案 > Ionic storage Uncaught TypeError: Cannot read property 'storage' of null error when gettin onesignal playerid

问题描述

我尝试将 onesignal playerid 保存到存储中。我在下面使用此代码;

        window["plugins"].OneSignal.getIds(function(ids) {
        console.log(ids.userId);
        this.storage.set('player_storage', ids.userId);
    });

我可以使用此代码获取 playerid 并在控制台上显示。但是当我尝试将 playerid 保存到存储中时,我得到了这个错误。

未捕获的类型错误:无法读取 null 的属性“存储”

PS:我之前尝试将数据发布到 php,但我也遇到了同样的错误。我的意思是任何操作都会返回此错误。

我需要这方面的帮助。

Login.ts 完整代码;

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { PostProvider } from '../../providers/post-provider';
import { Storage } from '@ionic/storage';
import { RegisterPage } from '../register/register';
import { HomePage } from '../home/home';
import { AlllogoutPage } from '../alllogout/alllogout';
import { Device } from '@ionic-native/device';


/**
 * Generated class for the LoginPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
})
export class LoginPage {

    login_username: string = "";
    login_password: string = "";
  login_error_msg: any;
  userdevicename: any;
  userdevicemodel: any;
  userdevicemanufacturer: any;
  login_player_id: any;
  loggedplayers: any = [];

  constructor(public navCtrl: NavController, public navParams: NavParams, private postPvdr: PostProvider, public storage: Storage, public device: Device) {
    this.userdevicename = this.device.uuid;
    this.userdevicemodel = this.device.model;
    this.userdevicemanufacturer = this.device.manufacturer;
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad LoginPage');
    console.log('Device UUID is: ' + this.device.isVirtual);
  }

  gotoreg() {
    this.navCtrl.push(RegisterPage);
  }

  alllogout() {
    this.navCtrl.push(AlllogoutPage, {loginkey: this.login_username});
  }

  addlogin() {
    if(this.login_username==""){
       this.login_error_msg = "4";
    }
    else if(this.login_password==""){
      this.login_error_msg = "5";
    }
    else {
      let body = {
        login_username: this.login_username,
        login_password: this.login_password,
        login_device: this.userdevicename,
        login_device_model: this.userdevicemodel,
        login_device_marka: this.userdevicemanufacturer,
        logdatas: 'do_login'
      };

      this.postPvdr.postData(body, 'http://www.website.com/login.php').subscribe((data) =>{
        var alertbox = data.msg;
        if (data.success) {

        var notificationOpenedCallback = function(jsonData) {
          console.log('notificationOpenedCallback:' + JSON.stringify(jsonData));
        };
        window["plugins"].OneSignal
          .startInit("xxx", "xxx")
          .handleNotificationOpened(notificationOpenedCallback)
          .endInit();

        window["plugins"].OneSignal.getIds(function(ids) {
            console.log(ids.userId);
        });

           this.storage.set('session_storage', data.result);
           this.navCtrl.setRoot(HomePage);
           this.login_error_msg = "6";
        }
        else {
           this.login_error_msg = alertbox;
        }
      });

    }
  }

}

标签: ionic3onesignalionic-storage

解决方案


推荐阅读