Node.js firebase aync等待在try catch中获取空对象
我正在为实时数据库开发firebase api,我正在使用async await,但它不适用于我在node js中使用的try catch,我可以看到我正在获取状态0,因此它调用catch,但在catch中,我将错误作为null对象,有人能帮我解释为什么我在这方面没有获得正确的错误吗?即使我的try代码是正确的,它仍然调用catch,在这里我添加了我的全部代码,任何人都可以查看我的代码,并帮助我解决这个错误吗?我得到了这样的回应 答复:Node.js firebase aync等待在try catch中获取空对象,node.js,firebase,firebase-realtime-database,google-cloud-firestore,google-cloud-functions,Node.js,Firebase,Firebase Realtime Database,Google Cloud Firestore,Google Cloud Functions,我正在为实时数据库开发firebase api,我正在使用async await,但它不适用于我在node js中使用的try catch,我可以看到我正在获取状态0,因此它调用catch,但在catch中,我将错误作为null对象,有人能帮我解释为什么我在这方面没有获得正确的错误吗?即使我的try代码是正确的,它仍然调用catch,在这里我添加了我的全部代码,任何人都可以查看我的代码,并帮助我解决这个错误吗?我得到了这样的回应 答复: {"status":0,"data":{}} API:
{"status":0,"data":{}}
API:
export const check_await = functions.https.onRequest(async (req, res) => {
try {
if (req.method === 'POST') {
const body_data = req.body;
const db = admin.database();
const org_id = body_data.company_id;
const highfive_id = body_data.highfive_id;
const ref = db.ref("organizations/" + org_id + "/highfive/" + highfive_id);
const snapshot = await ref.on("value");
const data = snapshot.val();
cors(req, res, () => { return res.send({ 'status': 1, 'data': data, 'msg': 'High five feed record get successfully' }); });
} else {
cors(req, res, () => { return res.send({ 'status': 0, 'msg': "Only POST method is allowed" }); });
}
} catch (error) {
cors(req, res, () => { return res.send({ 'status': 0, 'data': error }); });
}
});
最后我解决了这个问题,我需要使用
一次
而不是上的
,这是我的完整代码
export const check_await = functions.https.onRequest(async (req, res) => {
try {
if (req.method === 'POST') {
const body_data = req.body;
const db = admin.database();
const org_id = body_data.company_id;
const highfive_id = body_data.highfive_id;
const ref = db.ref("organizations/" + org_id + "/highfive/" + highfive_id);
const snapshot = await ref.once("value");
const data = snapshot.val();
cors(req, res, () => { return res.send({ 'status': 1, 'data': data, 'msg': 'High five feed record get successfully' }); });
} else {
cors(req, res, () => { return res.send({ 'status': 0, 'msg': "Only POST method is allowed" }); });
}
} catch (error) {
cors(req, res, () => { return res.send({ 'status': 0, 'data': error.message }); });
}
});