MagicMirrorOrg/MagicMirror · error · Error
setCallbacks() must be called before initialize()
Error message
setCallbacks() must be called before initialize()
What it means
openweathermap's initialize() requires callbacks to be registered first via setCallbacks(); it throws synchronously if onErrorCallback is not a function. This ensures the provider always has a channel to report later async failures (API key problems, fetch errors) instead of crashing unhandled.
Source
Thrown at defaultmodules/weather/providers/openweathermap.js:34
location: false,
lat: 0,
lon: 0,
apiKey: "",
type: "current",
updateInterval: 10 * 60 * 1000,
...config
};
this.fetcher = null;
this.onDataCallback = null;
this.onErrorCallback = null;
this.locationName = null;
}
initialize () {
// Validate callbacks exist
if (typeof this.onErrorCallback !== "function") {
throw new Error("setCallbacks() must be called before initialize()");
}
if (!this.config.apiKey) {
Log.error("[openweathermap] API key is required");
this.onErrorCallback({
message: "API key is required",
translationKey: "MODULE_ERROR_UNSPECIFIED"
});
return;
}
this.#initializeFetcher();
}
setCallbacks (onData, onError) {
this.onDataCallback = onData;
this.onErrorCallback = onError;
}View on GitHub (pinned to 4b4a59534f)
Solutions
- Call provider.setCallbacks(onDataCallback, onErrorCallback) before initialize()
- Ensure both callbacks are functions (not undefined/null)
- Reorder your bootstrap code so callback registration precedes initialize()
Example fix
// before provider.initialize(config); provider.setCallbacks(onData, onError); // after provider.setCallbacks(onData, onError); provider.initialize(config);
Defensive patterns
Strategy: try-catch
Validate before calling
if (typeof provider.onErrorCallback !== "function") {
throw new Error("Call setCallbacks(onData, onError) before initialize()");
}
provider.initialize(config); Type guard
const isCallbackFn = (f) => typeof f === "function";
Try / catch
try {
provider.initialize(config);
} catch (err) {
if (err.message.includes("setCallbacks() must be called before initialize()")) {
provider.setCallbacks(onData, onError);
provider.initialize(config); // retry once with correct order
} else throw err;
} Prevention
- Always register callbacks as the first provider call
- Wire setCallbacks(onDataCallback, onErrorCallback) with both arguments
- Enforce init order in a shared factory/bootstrap helper
- Code-review any upgrade that changes provider bootstrap
When it happens
Trigger: Calling initialize() before setCallbacks(), or calling setCallbacks() with missing/incorrect arguments so onErrorCallback stays undefined.
Common situations: Refactored init order after upgrade; forgetting the callbacks step in new wiring; passing setCallbacks(onData) with only one argument.
Related errors
- Unknown weather type: ${this.config.type}
- Unknown weather endpoint: ${this.config.weatherEndpoint}
- Latitude and longitude are required
- Unknown weather type: ${this.config.type}
- siteCode and provCode are required
AI-assisted analysis of MagicMirrorOrg/MagicMirror@4b4a59534f (2026-08-31).
Data as JSON: /api/errors/856885b404b0d2f4.
Report an issue: GitHub.