Hi Nico,
I used the GetDeviceInfo JS action from NativeMobileResources as template and this is with a little AI help what came out:
You're super close. Two issues:
mx.data.create
in Mendix is callback-based, not a Promise — await mx.data.create(...)
gives you undefined
.
Your action never returns the created object (you define createScreenSizeHelper
but never call it or return its result).
Here’s a fixed version that wraps mx.data.create
in a Promise, sets the attrs, and returns the MxObject
:
// This file was generated by Mendix Studio Pro.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
import "mx-global";
import { Big } from "big.js";
// BEGIN EXTRA CODE
// END EXTRA CODE
/**
* @returns {Promise.<MxObject>}
*/
export async function JS_GetScreenSize() {
// BEGIN USER CODE
const height = window?.screen?.height;
const width = window?.screen?.width;
if (height == null || width == null) {
throw new Error("Screen API not available: window.screen height/width is null or undefined");
}
console.debug(`Screen size detected: ${height}x${width}`);
// Create the object
const mxObject = await createMxObject("ModuleName.ScreenSizeHelper");
// If your attributes are Decimal, keep Big(); if they are Integer, set the raw numbers.
mxObject.set("Height", new Big(height)); // or: mxObject.set("Height", height);
mxObject.set("Width", new Big(width)); // or: mxObject.set("Width", width);
// Return the object so the action "returns an object" as expected
return mxObject;
// ---- helpers ----
function createMxObject(entity) {
return new Promise((resolve, reject) => {
mx.data.create({
entity,
callback: obj => resolve(obj),
error: e => reject(new Error(`Could not create '${entity}' object: ${e?.message ?? e}`))
});
});
}
// END USER CODE
}
Hey ;)
Maybe when you test the method your test user does not have the right access configured in order to inspect the object?
If it's not that I am also not sure. Please check the create / read access for your test user role.