Community Tip - You can subscribe to a forum, label or individual post and receive email notifications when someone posts a new topic or reply. Learn more! X
I'd like to programmatically add new Things, but I want to have some validation to see if the Thing is already Created.
The validation I want to do is something like this
if(Things["ThingName"] == null)
{
//Then Create the thing
}
but by doing that, I receive the error which tells me : "Cannot read Property "ThingName" from undefined"
Then seeing that, I Created another service that will only retrieve the Thing name / project Name
But both of them didn't work telling me the same error as before (Cannot read Property "ThingName" from undefined)
By the way, the thing is already created.
This is the code I used :
var result = Things["ThingName"].GetProjectName();
returning result as output
Then I tried with another Entity (tried ThingTemplate) and it worked.
So first, is the Things[""] bugged for me ?
Second, how could I add the validation I'm looking for?
Message was edited by: Frederik Grondin
Solved! Go to Solution.
You may look into try/catch. For example, the following will create a thing based on the already existing template (for this particular case), then if error occurs, it will delete to ensure no ghost entities were created. EDIT: or, since you are looking to create an entity if doesn't exist already, you may replace the deleteThing with CreateThing with the new name instead. The example is just a reference.
try {
var thing1 = {
thingTemplateName: "TestingTemplate"/* THINGTEMPLATENAME */,
name: InputName /* STRING */
};
// no return
Resources["EntityServices"].CreateThing(thing1);
} catch(err) {
var params = {
name: InputName /* THINGNAME */
};
// no return
Resources["EntityServices"].DeleteThing(params);
logger.error (err);
}
You may look into try/catch. For example, the following will create a thing based on the already existing template (for this particular case), then if error occurs, it will delete to ensure no ghost entities were created. EDIT: or, since you are looking to create an entity if doesn't exist already, you may replace the deleteThing with CreateThing with the new name instead. The example is just a reference.
try {
var thing1 = {
thingTemplateName: "TestingTemplate"/* THINGTEMPLATENAME */,
name: InputName /* STRING */
};
// no return
Resources["EntityServices"].CreateThing(thing1);
} catch(err) {
var params = {
name: InputName /* THINGNAME */
};
// no return
Resources["EntityServices"].DeleteThing(params);
logger.error (err);
}
I guess it would've worked, but I managed to find why my Things["Name"].XX wouldn't work.
The issue was, I had an Input called Things as well.
I guess it would take the input over the Entity Things Collection.