2016-10-12 166 views
2

我使用Sequelize連接到Postgres數據庫。我有這樣的代碼:使用Sequelize和ES6承諾?

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),這被記錄到控制檯:

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

.then((args) =>代碼塊在檢查站#2,args進來爲未定義。

如何獲取args以包含來自檢查點#1的結果數組?

回答

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))