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

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...