{"record":{"id":"a314b60576839466","repo":"RocketChat/Rocket.Chat","slug":"import-operation-not-initialized","errorCode":null,"errorMessage":"Import operation not initialized.","messagePattern":"Import operation not initialized\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/server/services/import/service.ts","lineNumber":98,"sourceCode":"\t\tconst operation = await Imports.findLastImport();\n\n\t\tif (!operation) {\n\t\t\treturn {\n\t\t\t\tstate: 'none',\n\t\t\t};\n\t\t}\n\n\t\tconst state = this.getStateOfOperation(operation);\n\n\t\treturn {\n\t\t\tstate,\n\t\t\toperation,\n\t\t};\n\t}\n\n\tprivate assertsValidStateForNewData(operation: IImport | undefined): asserts operation is IImport {\n\t\tif (!operation?.valid) {\n\t\t\tthrow new Error('Import operation not initialized.');\n\t\t}\n\t\tconst state = this.getStateOfOperation(operation);\n\t\tswitch (state) {\n\t\t\tcase 'loading':\n\t\t\tcase 'importing':\n\t\t\t\tthrow new Error('The current import operation can not receive new data.');\n\t\t\tcase 'done':\n\t\t\tcase 'error':\n\t\t\tcase 'canceled':\n\t\t\t\tthrow new Error('The current import operation is already finished.');\n\t\t}\n\t}\n\n\tpublic async addUsers(users: Omit<IImportUser, '_id' | 'services' | 'customFields'>[]): Promise<void> {\n\t\tif (!users.length) {\n\t\t\treturn;\n\t\t}\n","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/services/import/service.ts#L80-L116","documentation":"Thrown by the import service's assertsValidStateForNewData when Imports.findLastImport() returns no operation or one with valid !== true — i.e. no import operation has been started (or the last one is invalid). All add* methods (addUsers, addChannels, addMessages, ...) run this assertion before accepting data, so data uploads before an import is opened are rejected.","triggerScenarios":"Calling importService.addUsers(...) without a prior successful start/upload that created a valid IImport document; the last import record marked invalid by a failed upload; a fresh workspace where the Imports collection is empty.","commonSituations":"Importer flows that skip the file-upload/start step; restarts that lose in-memory assumptions while the database record was invalidated; concurrent import attempts where one cancels the operation record.","solutions":["Start a valid import operation first (the importer's upload/start flow must create it)","Re-run the upload step so Imports.findLastImport() returns a valid operation","Check the last import record before pushing data"],"exampleFix":"// before\nawait service.addUsers(users);\n\n// after\nconst op = await Imports.findLastImport();\nif (!op?.valid) throw new Error('Start an import operation first');\nawait service.addUsers(users);","handlingStrategy":"validation","validationCode":"const op = await Imports.findLastImport();\nif (!op?.valid) {\n  throw new Error('start an import operation before uploading data');\n}\nawait service.addUsers(users);","typeGuard":"function isValidImportOperation(op: IImport | undefined | null): op is IImport {\n  return Boolean(op && op.valid);\n}","tryCatchPattern":"try { await service.addUsers(users); } catch (e) { if (e.message === 'Import operation not initialized.') { /* run the upload/start flow, then retry once */ } }","preventionTips":["Always run the importer's start/upload step before pushing records","Check the last operation's validity before every batch"],"tags":["import","state-machine","migration","rocket-chat"],"backgroundTag":"invalid-operation-state","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-08-22T04:17:13.399Z"}