Returning value outside of function

I’m looking to use graphQL to query a docker machine api and get a list of containers for react docker admin style project. I’m using dockerode an NPM module to make the request. The first function getCOntainerById is how I would typically return some items from rethinkdb.

I can’t seem to figure out how to return the containers array in the docker.listContainers function as it’s only defined inside the scope and undefined at the end of the fetchContainers function return.

  • Windows Docker in PyCharm - How to locate files on my host computer?
  • How to restart kubernetes Pods?
  • Proper method to run gulp build on jenkins for AWS ECS
  • Pbs with python tempfile.mkdtemp in gitlab-ci docker
  • Pseudo-tty with Fig/Docker container
  • Cannot get new connection from pool
  • import Docker from 'dockerode'
    var docker = new Docker({host: 'http://127.0.0.1', port: 52376});
    
    export default {
      getContainerById: {
        type: Container,
        args: {
          id: {type: new GraphQLNonNull(GraphQLID)}
        },
        async resolve(source, {id}, {rootValue}) {
          isLoggedIn(rootValue);
          const container = await r.table('containers').get(id);
          if (!container) {
            throw errorObj({_error: 'Container not found'});
          }
          return container;
        }
      },
      fetchContainers: {
        type: new GraphQLList(Container),
        async resolve(source, {id}, {rootValue}) {
          isLoggedIn(rootValue);
          docker.listContainers(function(err, containers) {
    
          });
    
          if (!containers) {
            throw errorObj({_error: 'Container not found'});
          }
    
          return containers
        }
      }
    };
    

    Help would be greatly appreciated. Thank you.

  • Docker Compose Dependencies
  • How can i run java applications in docker using apache or tomcat server
  • Configure Dockerfile to set AWS configurations
  • Can't connect to MySQL docker container
  • Building common dependencies with docker-compose
  • Simplest way of passing all host environment variables to docker container
  • One Solution collect form web for “Returning value outside of function”

    async resolve(source, {id}, {rootValue}) {
      isLoggedIn(rootValue);
      const containers = await new Promise((resolve, reject) => {
        docker.listContainers((err, containers) => {
          if (err) reject(err);
          resolve(containers);
        });
      })
    
      if (!containers) {
        throw errorObj({_error: 'Container not found'});
      }
    
      return containers
    }
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.