'Type 'void | DiscordServer[]' is not assignable to type 'DiscordServer[]'
I'm getting this error while trying to call an async function. This is my code:
let result:DiscordServer[]
result = await Backendless.Data.of( backendlessDiscordServersTable! ).find<DiscordServer>( queryBuilder )
.catch( e => writeDiscordLog(filename, functionName, errMsg, e.toString()))
And the interface for DiscordServer is:
export interface DiscordServer {
objectId: string,
created?: Date,
ownerId?: string,
server_id?: string,
server_name?: string,
updated?: Date
}
Now this question got me to the right track: is not assignable to type 'string'. Type 'void' is not assignable to type 'string' Basically I understand that I need to return something in the catch.
So I fixed it like this:
result = await Backendless.Data.of( backendlessDiscordServersTable! ).find<DiscordServer>( queryBuilder )
.catch( e => {
writeDiscordLog(filename, functionName, errMsg, e.toString())
return result
})
The error is gone, but I'm wondering if this solution is correct, should I be returning result?
*** EDIT *** Just to clarify, what I'm not sure about is that when I return the var result inside a catch, what is it that I'm returning? Because if the function I'm calling is throwing an error, that error is returned in the catch. But result is of type DiscordServer[] so I'm not sure if I'll be having issues if the function throws an error and catch is executed. Hopefully I was able to explain my concern.
Also, as user kellys pointed out, I do have this code inside a try/catch block, I just wanted to simplify the code.
this is the code as I currently have it:
try {
result = await Backendless.Data.of( backendlessDiscordServersTable! ).find<DiscordServer>( queryBuilder )
.catch( e => {
writeDiscordLog(filename, functionName, errMsg, e.toString())
return result
})
if(result[0]){
console.log(result[0].server_id)
return result[0].server_id!
}
else{
console.log('doesnt exist')
return ''
}
} catch (error) {
throw error
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
