Referenceerror: hasOwner is not defined

Asked

Viewed 161 times

-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
  • 3

    const owner = await this.hasOwner(email)?

  • @bfavaretto if I use this.hasOwner(email), the error changes to Typeerror: Cannot read Property 'hasOwner' of Undefined

  • 1

    You tried to declare the function hasOwner out of class? Because if Voce is using within the class, without the this, it should be declared out. You should use the this as @bfavaretto mentioned, in this case of yours. Its function hasOwner and just like that? Because it seems that she always returns false.

  • @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?

1 answer

0


Resolved following the idea of the Cmte Cardinal.

I removed the class function and put it in a separate file utils.js and exporting the function. Within the class I did the file import and called the function through it.

utils.js

const axios = require('axios').default
const headers = { headers: { apikey: '...' } };

module.exports = {

    hasOwner: async function (email) {

        const contact = await (await axios.get(`https://api.moskitcrm.com/v1/contacts?limit=1&email=${email}`, headers)).data.results[0]

        console.log(contact)
        return (contact) ? contact : false
    }

}

Contactcontroller.js (The original store() code has been reduced to readability questions only)

const axios = require('axios').default
const headers = { headers: { apikey: '...' } };
const utils = require('../utils.js');

module.exports = class ContactController {

    async store(request, response) {
        const { email = [] } = request.body

        const owner = await utils.hasOwner(email)

        return response.json({ owner })
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.