I was trying to populate some test data in the database, in my Typescript / Typeorm backend project:
const createEntity = async <T extends EntityConstructor>(
Constructor: T,
input: Partial<InstanceType<T>>,
): Promise<InstanceType<T>> => {
const instance = Constructor.create(input);
return validateAndSaveEntity(instance as InstanceType<T>);
};
const seedIssues = (): Promise<Issue[]> => {
const issues = [
createEntity(Issue, {
title: 'Case 1'
}),
createEntity(Issue, {
title: 'Case 2'
}),
...
];
return Promise.all(issues);
};
The above works, but obviously I don't want to write a long long list containing the items. So I switched to using a for loop and append the items to the list:
const seedIssues = (): Promise<Issue[]> => {
let issues = []
for (let i = 0; i < 100; i++){
issues.push(
createEntity(Issue, {
title: 'Case ' + String(i),
})
)
}
return Promise.all(issues);
};
However, this code does not work and it throws this error:
src/database/createGuestAccount2.ts:172:7 - error TS2345: Argument of type 'Promise<Issue>' is not assignable to parameter of type 'never'.
172 createEntity(Issue, {
~~~~~~~~~~~~~~~~~~~~~
173 title: 'Case ' + String(i),
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
181 })
~~~~~~~~
at createTSError (/Users/api/node_modules/ts-node/src/index.ts:293:12)
at reportTSError (/Users/api/node_modules/ts-node/src/index.ts:297:19)
at getOutput (/Users/api/node_modules/ts-node/src/index.ts:399:34)
at Object.compile (/Users/api/node_modules/ts-node/src/index.ts:457:32)
at Module.m._compile (/Users/api/node_modules/ts-node/src/index.ts:530:43)
at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Object.require.extensions.<computed> [as .ts] (/Users/api/node_modules/ts-node/src/index.ts:533:12)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
What does it mean? How can I proceed to create many test data points with the for loop?