JavaScript: Created object does not return

0
Hi all,   I have a JavaScript action used to get the screen resolution. It should create a non-persistable object and populate the values. The object is then returned.   However, no object is returned. Why would it be? Here is the code we use   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 ScreenHeight = window.screen.height; const ScreenWidth = window.screen.width; console.debug('Screen size detected: ' + ScreenHeight + 'x' + ScreenWidth); async function createScreenSizeHelper(height, width) { try { const mxObject = await mx.data.create({ entity: "ModuleName.ScreenSizeHelper" }); if (!mxObject) { throw new Error("mxObject is null or undefined"); } mxObject.set("Height", height); mxObject.set("Width", width); return mxObject; } catch(err) { throw new Error("Could not create object:" + err.message) } } // END USER CODE }  
asked
2 answers
0

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:

  1. mx.data.create in Mendix is callback-based, not a Promise — await mx.data.create(...) gives you undefined.

  2. 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
}

 

answered
0

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.

answered