Return List of MxObjects from Java Script Action

0
Hi, I’m trying to create and return a list of MxObject Elements using a Nanoflow and JavaScript /** * @returns {Promise.<MxObject[]>} */ export async function CreatePhrasesList() { // BEGIN USER CODE return new Promise(function (resolve, reject) { let phrases = ["Phrase1", "Phrase2", "Phrase3", "Phrase4", "Phrase5", "Phrase6", "Phrase7"]; let list = []; for(let i=0; i<4; i++){ mx.data.create({ entity: "MyFirstModule.Phrase", callback: function(mxObject) { mxObject.set(mxObject.getAttributes()[0], phrases[i]); list.push(mxObject); }, error: function(e) { reject("Could not create object:" + error.message); } }); } resolve(list); }); // END USER CODE }   When executing the Nanoflow I’m getting an error    I would like not to commit these new MxObjects to the database but to use them a transient entities. Does anybody have a hint how to solve this? Thank you very much.   
asked
3 answers
5
function getList() 	{
    var phrases = ["Phrase", "Phrase2", "Phrase3", "Phrase4"];
    return Promise.all(phrases.map((phrase) => createObject(phrase)));
}

function createObject(text) {
    return new Promise((resolve, reject) => {
        mx.data.create({
            entity: "MyFirstModule.Phrase",
            callback: function(mxObject) {
                mxObject.set("Text", text);
                resolve(mxObject);
            },
            error: function(e) {
                reject("Could not create object:" + error.message);
            }
        });
    });
}

Use Promise.all to wait for multiple promises to complete.

answered
0
  1. NewPhrase is empty after the Head activity, so ReturnValueName is empty. Can you confirm that?
  2. If ReturnValueName is empty, does that mean list’ is empty? Do a console.log(list) before the resolve and one after push.
  3. You did not get an error message from the reject, so … oh wait:

Try to change 'let’ to 'var’

answered
0

I found the answer

// 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 { Big } from "big.js";

// BEGIN EXTRA CODE
// END EXTRA CODE

/**
 * @returns {Promise.<MxObject[]>}
 */
export async function CreatePhrasesList() {
	// BEGIN USER CODE
	return Promise.resolve(getList());

	function getList()
	{
		return new Promise(function (resolve, reject) {
			var phrases = ["Phrase", "Phrase2", "Phrase3", "Phrase4", "Phrase5", "Phrase6", "Phrase7", "Phrase8"];
			var list = [];

			for(let i=0; i<4; i++){
				mx.data.create({
					entity: "MyFirstModule.Phrase",
					callback: function(mxObject) {
						mxObject.set("Text", phrases[i]);
						list.push(mxObject);
					},
					error: function(e) {
						reject("Could not create object:" + error.message);
					}
				});
			}

			resolve(list);
		});
	}
	// END USER CODE
}

 

answered