2

In case I have

import axios from "axios";

function model() {
  function getAll() {
    return axios
      .get("http://localhost:3000/teams")
      .then(response => response.data);
  }
}

export default model;

How can I access getAll() method from another component ?

I tried importing model and then referring it to getAll - model.getAll(), but it complains that the method is undefined.

I tried referring to Calling a Function defined inside another function in Javascript , but could not find the solution.

Is this even the correct approach?

2
  • 1
    return getAll from model() Commented Oct 22, 2018 at 13:54
  • Maybe you can put teams in a seperate file and export it: export default axios.get(.... and just import teams import teams from './teams';teams.then(teams=>.... Commented Oct 22, 2018 at 13:54

2 Answers 2

4

You can't access getAll from anywhere except inside model. Maybe you meant to create an object?

var model = {
    getAll: function () { ... }
}

model.getAll();
Sign up to request clarification or add additional context in comments.

1 Comment

For some reason I thought I was meant to do it this way. In case I was doing it as I normally do, I would just export the function and then import it. Since it's not possible, I must've not figured it out correctly. Thanks
1

You could always instantiate the function

function model() {
  this.getAll = () => {
    console.log("hello world");
  };
}

const myFunc = new model();

myFunc.getAll() // console.log('hello world')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.