-1
Hello, I am working on a Node application using mainly express and Axios. I am using controllers and within one of these created a function and within that function, I call one another, however, this second is being given as undefined.
Contactcontroller.js
const axios = require('axios').default
const headers = { headers: { apikey: '...' } };
module.exports = class ContactController {
// Busca um contato por email, se encontrado, retorna seu id
async hasOwner(email) {
await axios.get(`https://api.moskitcrm.com/v1/contacts?limit=1&email=${email}`, headers)
.then((response) => {
return response.data.results[0]
})
return false
}
async store(request, response) {
const { name, notes, email = [], phone = [], deal = 'Nova Oportunidade', product, cpf, ps, language, modality, unit, course, price = 250000 } = request.body
if (!name) return response.status(400).json({ message: 'Nome não pode ser nulo' })
const stage = product === 145474 ? 126877 : 128183
// O erro acontece ao realizar a chamada
const owner = await hasOwner(email)
return response.json(owner)
...
}
}
Error
(node:4344) UnhandledPromiseRejectionWarning: ReferenceError: hasOwner is not defined
const owner = await this.hasOwner(email)
?– bfavaretto
@bfavaretto if I use this.hasOwner(email), the error changes to Typeerror: Cannot read Property 'hasOwner' of Undefined
– Gabriel Antunes
You tried to declare the function
hasOwner
out of class? Because if Voce is using within the class, without thethis
, it should be declared out. You should use thethis
as @bfavaretto mentioned, in this case of yours. Its functionhasOwner
and just like that? Because it seems that she always returnsfalse
.– Cmte Cardeal
@Cmtecardeal didn’t try. I tried declaring her in function and calling with this, yet it didn’t work. Suggest I create in another file and import it in this class?
– Gabriel Antunes