Example: Creation and Deletion of Ghost Entities
The following example results in the creation of a Ghost Thing:
var params = {
name: "GhostThing" /* STRING */,
description: "Ghost" /* STRING */,
thingTemplateName: "GenericThing" /* THINGTEMPLATENAME */
};

// Successfully creates the GhostThing entity.
Resources["EntityServices"].CreateThing(params);
// Code that will cause an exception to be thrown by the service.
var makeError;
// Exception is thrown as the notThere property does not exist
on the makeError variable at this point.
makeError.notThere = 1;
Use the “try-catch” mechanism to delete ghost entities:
try {
var params = {
name: "GhostThing" /* STRING */,
description: "Ghost" /* STRING */,
thingTemplateName: "GenericThing" /* THINGTEMPLATENAME */
};
Resources["EntityServices"].CreateThing(params);
// Any code that could potentially cause an exception should
// be included in the try-catch block.

// The following code will cause an exception to be thrown
// as makeError.notThere has not been defined.
var makeError;
makeError.notThere = 1;

} catch (err) {
// If an exception is caught, we need to delete the entity
// that was created to prevent it from becoming a Ghost Entity
var params = {
name: "GhostThing" /* THINGNAME */
};
Resources["EntityServices"].DeleteThing(params);
}
Was this helpful?