Android 全局变量范围返回[Object Object],而不是node.js中的字符串
我试图访问请求函数中的tokenName,我能够成功地得到响应。但我想全局访问它,以便将其保存在数据库中Android 全局变量范围返回[Object Object],而不是node.js中的字符串,android,node.js,Android,Node.js,我试图访问请求函数中的tokenName,我能够成功地得到响应。但我想全局访问它,以便将其保存在数据库中 request(options,function (error, response, body) { tokenName = body.notification_key; console.log('tokenName: ' + tokenName); //I get the token name successfully return tokenName;
request(options,function (error, response, body) {
tokenName = body.notification_key;
console.log('tokenName: ' + tokenName); //I get the token name successfully
return tokenName;
});
var data = {
image: image,
email: token_email,
name: name1,
notification_key: tokenName, //now, here it returns [Object object],dont know why
token_id: token_id1,
extra: 'created'
};
console.log('image : ' + image);
console.log('email : ' + token_email);
console.log('name : ' + name1);
console.log('tokenName : ' + tokenName); //gives[Object object]
console.log('token_id : ' + token_id1);
return db.collection('Users').doc(user_id).set(data); //could not save because of this
JSON的主体类似于
{ notification_key: ‘WPX91bFxpiCMFe5p6JjypsOSgXn2lCVHMrX5Q1d-fjYqFoHMMc-
DjE8S97GJiCs0lw0DPGnckSGe_AQhhOViV5MF67Rodb9bNlCPmYt2UUi2-
vPmrwncYJs7NqdZE7DyuO3sZ0e_b98c’ }
我不擅长node.js。您应该全局定义“tokenName”以在函数外部使用它,并且您应该将tokenName转换为string以将其保存在数据库中,如下所示:-
let tokenName; // i set this variable at global level
request(options,function (error, response, body) {
tokenName = body.notification_key; // asing the valeu to the global variable
console.log('tokenName: ' + tokenName);
});
var data = {
image: image,
email: token_email,
name: name1,
notification_key: tokenName, //now it return token (string)
token_id: token_id1,
extra: 'created'
};
console.log('image : ' + image);
console.log('email : ' + token_email);
console.log('name : ' + name1);
console.log('tokenName : ' + tokenName);
console.log('token_id : ' + token_id1);
return db.collection('Users').doc(user_id).set(data);
它还是一样的。我还观察到数据负载在请求函数之前运行。你可以在我用图片更新帖子时查看。我觉得你不应该返回tokenName,而是应该将其设置为全局并直接使用,这样就不需要将其转换为stringPlease read-总结是,这不是向志愿者致辞的理想方式,可能会对获得答案产生反作用。请不要将此添加到您的问题中。