我刚开始使用Cloud Functions for Firebase。我一直坚持从数据库中检索数据。
我在Firebase引用上使用.once('value')
将其转换为promise,但每次我使用Postman运行该函数时都会收到此错误:
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:357:11)
at ServerResponse.header (/usr/lib/node_modules/firebase-tools/node_modules/express/lib/response.js:725:10)
at ServerResponse.send (/usr/lib/node_modules/firebase-tools/node_modules/express/lib/response.js:170:12)
at ServerResponse.json (/usr/lib/node_modules/firebase-tools/node_modules/express/lib/response.js:256:15)
at Timeout.req.functionTimeout.setTimeout [as _onTimeout] (/usr/lib/node_modules/firebase-tools/node_modules/@google-cloud/functions-emulator/src/supervisor/supervisor.js:192:14)
at ontimeout (timers.js:380:14)
at tryOnTimeout (timers.js:244:5)
at Timer.listOnTimeout (timers.js:214:5)
这是第一个调用函数获取数据的函数:
exports.userPlaylistStatus = functions.https.onRequest(async (request, response) => {
const userId = request.body.userId;
// Wait to get the user reference
const userRef = await buildUserRef(userId);
response.send(userRef);
});
这是我正在使用.once('value')
的函数。承诺永远不会返回值。即使我在console.log('hi')
内放置.then()
,也永远不会记录。
async function buildUserRef(_userId: string) {
return await admin.database().ref(`/users/${_userId}`).once('value').then(
// this would return the user reference but I have it as .val() for now
res => {return res.val();},
err => {console.log(err)}
);
}