Reputation: 539
I have a cloud function and when I use some async functions and put a 'return' in each possible output, I still get Not all code paths return a value
I've tried deleting my database calls and just having the 'return {data:{...}};' which makes the error go away.
I have also tried wrapping everything inside a 'try' 'catch' block.
I current have what I would expect to work which is two blocks get().then()...catch()..
export const getUsersInHere = functions.https.onCall((data, context) =>
{
if(!context || !context.auth || !context.auth.uid)
{
return {data:{message:"Please login again...", success:false}};
}
else if(data)
{
const my_uid = context.auth.uid;
db.collection(`InHere/${my_uid}`).get().then(snapshot =>
{
return {data:{}, success:true};
}).catch(e =>
{
return {data:{message:"No last message yet...", success:false}};
});
}
else
{
return {data:{message:"no body sent", success:false}};
}
});
I would expect to be able to deploy my cloud function with firebase deploy, instead I am getting deploy errors:
src/index.ts:83:62 - error TS7030: Not all code paths return a value.
83 export const getUsersInHere = functions.https.onCall((data, context) =>
Note I think I've found that 'firestore deploy' works when I added 'async' into the signature of the callable, however the 'warning/error' still remains in Microsoft Studio Code (Not all code paths return a value.ts(7030))
export const getUsersInThisChatRoom = functions.https.onCall(async (data, context) =>
Upvotes: 1
Views: 2541
Reputation: 317382
With callables, you can either directly return the object to be serialized and sent to the client, or you can return a promise that resolves with the object to send. All you have to do is return the promise in your else if
block:
// note the return added to the next line
return db.collection(`InHere/${my_uid}`).get().then(snapshot =>
{
return {data:{}, success:true};
}).catch(e =>
{
return {data:{message:"No last message yet...", success:false}};
});
This will return the promise that resolves to either value you returned from the then
or catch
callback.
You have no obligation to use async/await, but if you do, you should completely replace your then and catch blocks with proper async/await syntax. It will look very different.
Upvotes: 1