微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 为什么我的承诺链失败了?

我正在尝试使用knex来播种数据库.在贡献者的帮助下,我成功地在一张桌子上播种了我需要采取的几个步骤:

>我需要从几个外部表中提取id值,这样我就可以填充种子表的外键值.
>在种子表中生成n条记录.
>填充表格.

如上所述,我在一张桌子上工作.因为我比一个屋顶大头钉更聪明,并且必须为另一个表做几乎完全相同的事情,我只是复制了第一个种子文件中的工作,将其放入第二个种子文件中,进行了一些适当的修改(具体而言),在第二个表中,我只需要在此时填充1个外键值)并且……它不起作用.

我不知所措.当然,我在这代码中遗漏了一些愚蠢的小东西,但我找不到它.我正在尝试播种一个单元表,我必须使用properties.id值填充它.

exports.seed = function(knex,Promise) {
    console.log('seeding the %s table...',tableName);

    Promise.resolve([
        'properties',])
    .map(function(table) {
        // Pull foreign key values (property_id)
        var ids = knex.select('id').from(table).pluck('id');

        // AT THIS POINT THE ids VARIABLE HAS A VALUE

        return ids;
    })
    .spread(function(properties) {
        // BUT I NEVER SEE THIS LOG PRINT
        console.log('SPREADING UNITS');
    });
};

我究竟做错了什么?我把.catch()和.error()放到了这个东西里,但没有任何东西写入日志.不知何故,我似乎永远不会陷入.spread(…)方法.

UPDATE

无论它值多少,这都是.map方法返回之前的内容……

{ client:
   { Formatter: { [Function: Formatter_MysqL] super_: [Function: Formatter] },Raw: { [Function: Raw_MysqL] super_: [Object] },Transaction: { [Function: Transaction_MysqL] super_: [Object] },QueryBuilder: { [Function: QueryBuilder_MysqL] super_: [Object] },QueryCompiler: { [Function: QueryCompiler_MysqL] super_: [Function: QueryCompiler] },migrationConfig: { tableName: 'knex_migration',directory: './migrations' },seedConfig: { directory: './seeds' },Runner: { [Function: Runner_MysqL] super_: [Function: Runner] },connectionSettings:
      { host: '127.0.0.1',port: '3306',user: 'root',password: '',database: 'realster',timezone: 'UTC',charset: 'utf8',debug: false },Pool: { [Function: Pool_MysqL] super_: [Function: Pool] },databaseName: 'realster',pool: { client: [Circular],config: [Object],genericPool: [Object] },_events: { start: [Function],query: [Function] },Seeder: { [Function: Seeder_MysqL] super_: [Function: Seeder] } },_single: { table: 'properties',pluck: 'id' },_statements:
   [ { grouping: 'columns',value: [Object] },{ grouping: 'columns',type: 'pluck',value: 'id' } ],_errors: [],_joinFlag: 'inner',_boolFlag: 'and',_notFlag: false,and: [Circular],_method: 'pluck' }

解决方法

您在地图的结果中使用spread,这是Promise的方法.

该映射可能会生成一个数组,该数组没有spread()函数.

另外,spread通常会收到带有多个参数的回调.如果你需要一个数组,那么只需使用旧的.

总之,我猜您的代码看起来应该更像这样:

var mapProperties = function(table) {
  return knex.select('id').from(table).pluck('id');
};

Promise.resolve([
  'properties'
])
.then(mapProperties)
.then(function(properties) {
  console.log(properties);
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐