2 Replies Latest reply on Feb 16, 2017 11:04 AM by fgrondin RSS
    fgrondin Explorer

    Programmatically add Things

    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

      • Re: Programmatically add Things
        polinao Collaborator

        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);

         

        }