Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript类中的数组属性_Javascript_Node.js_Reactjs_Firebase - Fatal编程技术网

javascript类中的数组属性

javascript类中的数组属性,javascript,node.js,reactjs,firebase,Javascript,Node.js,Reactjs,Firebase,我在这个javascript类中声明了两个属性,第一个属性有问题。我正在尝试在一个包含node.js和react.js的项目中使用Firebase处理令牌 export default class NoNotificationResource { allTokens = []; tokensLoaded = false; constructor(messaging, database) { this.database = database;

我在这个javascript类中声明了两个属性,第一个属性有问题。我正在尝试在一个包含node.js和react.js的项目中使用Firebase处理令牌

export default class NoNotificationResource {
    allTokens = [];
    tokensLoaded = false;
    constructor(messaging, database) {
        this.database = database;
        this.messaging = messaging;
        try {
            this.messaging.requestPermission().then(res => {
                console.log('Permiso concedido');
            }).catch(err => {
                console.log('no acces', err);
            });
        } catch (err) {
            console.log('NO hay soporte para notificaciones', err);
        };
        this.setupTokenRefresh();
        this.database.ref('/fcmTokens').on('value', snapshot => {
            this.allTokens = snapshot.val();
            this.tokensLoaded = true;
        })
    };
    setupTokenRefresh(){
        this.messaging.onTokenRefresh(() => {
            this.saveTokenToServer();
        })
    };
    saveTokenToServer(){
        this.messaging.getToken().then(res => {
            if (this.tokensLoaded) {
                const existingToken = this.findExistingToken(res);
                if (existingToken) {
                    //Reemplazar token
                } else {
                    //Crear nuevo token
                }
            }
        });
    }
    findExistingToken(tokenToSave){
        for(let tokenKey in this.allTokens){
            const token = this.allTokens[tokenKey].token;
            if (token === tokenToSave) {
                return tokenKey;
            }
        }
        return false;
    }
}

如果没有linter,它不会显示任何错误(我修改了您的代码,这样它就不会查找无法访问的服务)

唯一的变化是我将
allTokens
移动到构造函数中-更多:

我添加了一个“手动”setToken来显示
this.allTokens
可以工作

“严格使用”;//只是为了确保
类非通知资源{
tokensLoaded=false;
构造函数(消息传递、数据库){
this.allTokens=[];
this.database=数据库;
this.messaging=消息传递;
试一试{
this.messaging.requestPermission().then(res=>{
console.log('Permiso concedido');
}).catch(错误=>{
console.log(“无访问”,错误);
});
}捕捉(错误){
日志('NO hay soport para notificaciones',err);
};
如果(此消息传递){
this.setupTokenRefresh();
this.database.ref('/fcmTokens')。on('value',snapshot=>{
this.allTokens=snapshot.val();
this.tokensLoaded=true;
})
}
};
setupTokenRefresh(){
if(this.messaging&&this.messaging.onTokenRefresh){
this.messaging.onTokenRefresh(()=>{
这是saveTokenToServer();
})
}
};
saveTokenToServer(){
this.messaging.getToken().then(res=>{
如果(此标记已标记){
const existingToken=this.findExistingToken(res);
如果(现有令牌){
//重采样令牌
}否则{
//克雷尔新代币
}
}
});
}
findExistingToken(tokenToSave){
for(让tokenKey输入此.allTokens){
const token=this.allTokens[tokenKey].token;
如果(令牌===令牌保存){
返回令牌密钥;
}
}
返回false;
}
//“手动”设置令牌
setToken(val){
//检查令牌是否已在allTokens数组中
//如果没有,只添加令牌
如果(!this.allTokens.includes(val))this.allTokens.push(val)
还这个
}
}
const n=新的非通知资源()
n、 findExistingToken(123)

console.log(n.setToken(123))
您可以在类外声明这两个变量,即
所有标记
标记。
例:-

或者您可以像这样将其放入构造函数中
this.allTokens=[]
this.tokensLoaded=false

let allTokens = [];
let tokensLoaded = false;
export default class NoNotificationResource {
    constructor(messaging, database) {
        this.database = database;
        this.messaging = messaging;
        try {
            this.messaging.requestPermission().then(res => {
                console.log('Permiso concedido');
            }).catch(err => {
                console.log('no acces', err);
            });
        } catch (err) {
            console.log('NO hay soporte para notificaciones', err);
        };
        this.setupTokenRefresh();
        this.database.ref('/fcmTokens').on('value', snapshot => {
            this.allTokens = snapshot.val();
            this.tokensLoaded = true;
        })
    };
    setupTokenRefresh(){
        this.messaging.onTokenRefresh(() => {
            this.saveTokenToServer();
        })
    };
    saveTokenToServer(){
        this.messaging.getToken().then(res => {
            if (this.tokensLoaded) {
                const existingToken = this.findExistingToken(res);
                if (existingToken) {
                    //Reemplazar token
                } else {
                    //Crear nuevo token
                }
            }
        });
    }
    findExistingToken(tokenToSave){
        for(let tokenKey in this.allTokens){
            const token = this.allTokens[tokenKey].token;
            if (token === tokenToSave) {
                return tokenKey;
            }
        }
        return false;
    }
}