Reputation: 41236
I'm trying to seed a database using knex. With the help of a contributor, I've successfully seeded one table where I need to take several steps:
As mentioned, I have this working for one table. Because I'm smarter than a roofing tack and have to do almost the exact same thing for another table, I just copied what was working in the first seed file, dropped it into the second seed file, made a couple of appropriate modifications (specifically, in the second table I only have to populate 1 foreign key value at this point) and...it doesn't work.
I'm at a loss. Surely there's some stupid little thing I'm missing in this code, but I can't find it. I'm trying to seed a units
table that I have to populate with a properties.id
value.
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');
});
};
What am I doing wrong? I've dropped .catch()
and .error()
into this thing, but nothing gets written to the log. Somehow I just never seem to drop into the .spread(...)
method.
UPDATE
For whatever it's worth, this is the content of the .map
method before it's returned...
{ 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' }
Upvotes: 4
Views: 504
Reputation: 2854
You are using spread
, which is a method of Promise
, in the result of a map
.
The map
probably produces an array, which doesn't have a spread()
function.
Also, spread
usually receives a callback with multiple arguments. If you need an array, simply use good old then
.
In summary, I guess your code should look more like this:
var mapProperties = function(table) {
return knex.select('id').from(table).pluck('id');
};
Promise.resolve([
'properties'
])
.then(mapProperties)
.then(function(properties) {
console.log(properties);
});
Upvotes: 1