Javascript 从Firebase的云函数读取数据?
我正在使用以下代码:Javascript 从Firebase的云函数读取数据?,javascript,firebase,firebase-realtime-database,google-cloud-functions,Javascript,Firebase,Firebase Realtime Database,Google Cloud Functions,我正在使用以下代码: exports.lotteryTickets = functions.database.ref('/lottery/ticketsneedstobeprocessed/{randomID}').onWrite(event => { let ticketsBoughtByUser = event.data.val(); }) 但是ticketsBoughtByUser是不正确的。我如何检索下图所示的数字,所以在字符串(oeb…)旁边?多谢各位 我得到这个日
exports.lotteryTickets = functions.database.ref('/lottery/ticketsneedstobeprocessed/{randomID}').onWrite(event => {
let ticketsBoughtByUser = event.data.val();
})
但是ticketsBoughtByUser是不正确的。我如何检索下图所示的数字,所以在字符串(oeb…)旁边?多谢各位
我得到这个日志:在您的例子中,
event.data.val()
显然不返回数字。它返回您在日志中看到的对象。如果console.log(ticketsBoughtByUser)
(不要使用字符串连接来构建消息),您实际上可以看到对象中的数据
对于数据库中显示的数据,我希望val是包含此数据的对象(经过编辑,因此我不必键入):
如果要从该对象中获取1
,则必须使用字符串键进入该对象,无论该字符串表示什么:
const num = ticketsBoughtByUser["oeb...IE2"]
如果只需要数字,而不需要原始位置的对象,则需要两个通配符来直接获取:
exports.lotteryTickets = functions.database
.ref('/lottery/ticketsneedstobeprocessed/{randomID}/{whatIsThis}')
.onWrite(event => {
const num = event.data.val()
}
我为whatIsThis
添加了一个通配符,它将匹配我上面编辑的字符串
但是我不知道你的函数想要完成什么,所以这只是猜测你是否应该这样做。在你的例子中,
event.data.val()
显然不返回数字。它返回您在日志中看到的对象。如果console.log(ticketsBoughtByUser)
(不要使用字符串连接来构建消息),您实际上可以看到对象中的数据
对于数据库中显示的数据,我希望val是包含此数据的对象(经过编辑,因此我不必键入):
如果要从该对象中获取1
,则必须使用字符串键进入该对象,无论该字符串表示什么:
const num = ticketsBoughtByUser["oeb...IE2"]
如果只需要数字,而不需要原始位置的对象,则需要两个通配符来直接获取:
exports.lotteryTickets = functions.database
.ref('/lottery/ticketsneedstobeprocessed/{randomID}/{whatIsThis}')
.onWrite(event => {
const num = event.data.val()
}
我为whatIsThis
添加了一个通配符,它将匹配我上面编辑的字符串
但是我不知道你的函数想要实现什么,所以这只是猜测你是否真的应该这样做。你也可以得到ticketsBoughtByUser值,如下所示
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/articles/{articleId}')
.onWrite(event => {
// Grab the current value of what was written to the Realtime Database.
var eventSnapshot = event.data;
//Here You can get value through key
var str = eventSnapshot.child("author").val();
console.log(str);
});
您还可以获得ticketsBoughtByUser值,如下所示
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/articles/{articleId}')
.onWrite(event => {
// Grab the current value of what was written to the Realtime Database.
var eventSnapshot = event.data;
//Here You can get value through key
var str = eventSnapshot.child("author").val();
console.log(str);
});
你怎么知道是楠?您现在只在函数中显示了一行代码,因此不可能看到真正的情况。console.log(ticketsBoughtByUser),然后我转到firebase函数,我看到了该日志。这是整个功能。我只想得到“1”,或者任何其他可能出现的整数。。。当然,控制台日志在let ticketsBoughtByUser声明之后。很抱歉,当我尝试以下操作时出现了NaN:Number(event.data.val();)。然而,我在我的问题中添加了以下日志。你是如何看到它是NaN的?您现在只在函数中显示了一行代码,因此不可能看到真正的情况。console.log(ticketsBoughtByUser),然后我转到firebase函数,我看到了该日志。这是整个功能。我只想得到“1”,或者任何其他可能出现的整数。。。当然,控制台日志在let ticketsBoughtByUser声明之后。很抱歉,当我尝试以下操作时出现了NaN:Number(event.data.val();)。但是,我在问题中添加了以下日志。