2016-05-03 12 views
0

graphockを使用してドッカーのマシンapiに問い合わせ、反応ドッカーの管理スタイルプロジェクトのコンテナのリストを取得したいと考えています。私は要求を行うためにNPMモジュールdockerodeを使用しています。最初の関数getCOntainerByIdは、私がrethinkdbからいくつかの項目を返す方法です。関数の外で値を返す

docker.listContainers関数でコンテナ配列を返す方法を理解できないようですが、スコープ内でのみ定義され、fetchContainers関数の終わりには未定義です。

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 
    } 
    } 
}; 

助けていただければ幸いです。ありがとうございました。

答えて

1
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 
} 
+0

ありがとうございました!いつもJSで学ぶべきことがあるようだ。 – ncrmro