我正在尝试向Firebase数据库发出请求,运行以下代码:
const lastCrawl = admin
.database()
.ref('/crawls')
.orderByChild("dateAdded")
.limitToLast(1)
.once('value')
.then((snapshot) => {
return snapshot.val();
});
我根据文档编写了这篇文章,并添加了一些额外的步骤,只传递目标目录中的最后一条记录。然而,即使对目录中的一条记录进行硬编码(例如:'/crawls/RECORD-ID'
(也会引发相同的错误,这很奇怪,因为代码与文档中的代码基本相同。
目前它返回以下错误:
Unhandled rejection
TypeError: obj.hasOwnProperty is not a function
at each (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:553:17)
at validateFirebaseData (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:1511:9)
at /srv/node_modules/@firebase/database/dist/index.node.cjs.js:1528:13
at each (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:554:13)
at validateFirebaseData (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:1511:9)
at /srv/node_modules/@firebase/database/dist/index.node.cjs.js:1528:13
at each (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:554:13)
at validateFirebaseData (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:1511:9)
at /srv/node_modules/@firebase/database/dist/index.node.cjs.js:1528:13
at each (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:554:13)
正如Frank所评论的,有一个根本问题,我试图从回调中return
,因为它是异步调用的,所以无法工作。
我决定解决的方法是返回如下所做的承诺:
const getLastCrawl = async () => {
const ref = admin.database().ref('/crawls/')
return ref.orderByChild("dateAdded")
.limitToLast(1)
.once('value')
.then((snapshot) => {
return snapshot.val();
});
}
const lastCrawl = await getLastCrawl()
非常感谢弗兰克的帮助!