I’m using adonisjs 6 and have created a service to check if a photo exists. If it exists, it will send the correct title of the photo, if it does not exist, it will return an already stored photo.
import superagent from 'superagent'
import env from '#start/env'
class VerifyPhotoService {
API_URL = env.get('API_URL')
async getPhoto(file: string) {
const buildPhotoUrl = (filename: string) => `${this.API_URL}uploads/user-photo/${filename}`
try {
const res = await superagent.get(buildPhotoUrl(file))
if (res.statusCode === 200) {
return this.API_URL + 'uploads/user-photo/' + file
} else {
return this.API_URL + 'uploads/default/sky.png'
}
} catch (error) {
console.error('Error al obtener la foto:', error)
return this.API_URL + 'uploads/default/sky.png'
}
}
}
const verifyPhoto = new VerifyPhotoService()
export default verifyPhoto
Everything is OK until now. The problem occurs is that when I go to the “user” model I want the url to appear as another field in the api. For that I am using the serializeExtras() function.
async filePhoto() {
try {
const verifiedPhotoUrl = await verifyPhoto.getPhoto(this.photo)
return verifiedPhotoUrl
} catch (error) {
console.error('Error verifying photo:', error)
return '/path/to/default/photo.png'
}
}
serializeExtras() {
console.log(this.filePhoto())
return {
photoUrl: this.filePhoto(),
}
It is not returning the url but rather a missing promise(Promise { }
). If I add it to the async function it stops working and does not appear in the return.
Any suggestions? Thank you
I want it to return the correct value.
I tried changing the function to asynchronous but it is not fulfilling its function
Jose Camejo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.