2016-10-12 5 views
2

私はSequelizeを使用してPostgresデータベースに接続しています。私はこのコードを持っている:最初の.thenブロックでES6約束でSequelizeを使用しますか?

return Promise.resolve() 
    .then(() => { 
     console.log('checkpoint #1'); 
     const temp = connectors.IM.create(args); 
     return temp; 
    }) 
    .then((x) => console.log(x)) 
    .then((args) =>{ 
     console.log(args); 
     args = Array.from(args); 
     console.log('checkpoint #2'); 
     const temp = connectors.IM.findAll({ where: args }).then((res) => res.map((item) => item.dataValues)) 
      return temp; 
     } 
    ) 
    .then(comment => { 
     return comment; 
    }) 
    .catch((err)=>{console.log(err);}); 

チェックポイント#1にし、新しいレコードが正常にPostgresデータベースに追加されます。その後、次のブロックでconsole.log(x)では、これは、コンソールに記録されます:.then((args) =>コードブロックで

{ dataValues: 
{ id: 21, 
    fromID: '1', 
    toID: '2', 
    msgText: 'Test from GraphIQL', 
    updatedAt: Wed Oct 12 2016 09:52:05 GMT-0700 (PDT), 
    createdAt: Wed Oct 12 2016 09:52:05 GMT-0700 (PDT) }, 
_previousDataValues: 
{ fromID: '1', 
    toID: '2', 
    msgText: 'Test from GraphIQL', 
    id: 21, 
    createdAt: Wed Oct 12 2016 09:52:05 GMT-0700 (PDT), 
    updatedAt: Wed Oct 12 2016 09:52:05 GMT-0700 (PDT) }, 
_changed: 
{ fromID: false, 
    toID: false, 
    msgText: false, 
    id: false, 
    createdAt: false, 
    updatedAt: false }, 
'$modelOptions': 
{ timestamps: true, 
    instanceMethods: {}, 
    classMethods: {}, 
    validate: {}, 
    freezeTableName: false, 
    underscored: false, 
    underscoredAll: false, 
    paranoid: false, 
    rejectOnEmpty: false, 
    whereCollection: null, 
    schema: null, 
    schemaDelimiter: '', 
    defaultScope: {}, 
    scopes: [], 
    hooks: {}, 
    indexes: [], 
    name: { plural: 'IMs', singular: 'IM' }, 
    omitNul: false, 
    sequelize: 
    { options: [Object], 
    config: [Object], 
    dialect: [Object], 
    models: [Object], 
    modelManager: [Object], 
    connectionManager: [Object], 
    importCache: {}, 
    test: [Object], 
    queryInterface: [Object] }, 
    uniqueKeys: {}, 
    hasPrimaryKeys: true }, 
'$options': 
{ isNewRecord: true, 
    '$schema': null, 
    '$schemaDelimiter': '', 
    attributes: undefined, 
    include: undefined, 
    raw: undefined, 
    silent: undefined }, 
hasPrimaryKeys: true, 
__eagerlyLoadedAssociations: [], 
isNewRecord: false } 

をチェックポイント#2で、argsは未定義で提供されます。

どのようにして、チェックポイント#1の結果の配列を含むようにargを取得しますか?

答えて

5
.then((x) => console.log(x)) 
.then((args) =>{ 

.then((x) => { 
    console.log(x); 

    return undefined; 
}) 
.then((args) =>{ 

console.logため、戻りundefinedをしているようなものです。つまり、undefinedの値は次の.thenに渡される値になります。

最も簡単な方法は、明示的に

.then((x) => { 
    console.log(x); 

    return x; 
}) 

またはコンマ演算子に

.then((x) => (console.log(x), x)) 
を使用して短いバージョンでだろう
関連する問題