Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

usergrid nodejs issue with createEntity and getOnExist

Hi guys, I am working on Nodejs to connect with Baas and do CRUD operations.

In POST, I am using createEntity call and posting data with a name parameter.

Now when I post another entity with same name, I am getting pre-existing entity, instead of an error.

In github, I found this,

Wait! But what if my entity already exists on the server?
During a client.createEntity call, there are two ways that you can choose to handle this situation. The question is, what should the client do if an entity with the same name, username, or uuid already exists on the server?
1. Give you back an error.
2. Give you back the pre-existing entity.
If you want to get back an error when the entity already exists, then simply call the client.createEntity function as above. If there is a collision, you will get back a 400 However, if you want the existing entity to be returned, then set the getOnExist flag to true:
var options = {
    type:'dogs',
    name:'Dino',
    getOnExist:true
}
client.createEntity(options, function (err, dog) {
    if (err) {
        //error - dog not created
    } else {
        //success -dog is created or returned, depending on if it already 

So I used getOnExist : false but I am still getting the pre-existing entity. @Dino @Anil Sagar Any ideas what to do?

I have tried below options,

var options = {
    type:'dogs',
    name:'Dino',
    getOnExist:false
}


var options = {
    type:'dogs',
    name:'Dino',
}
Solved Solved
1 4 268
1 ACCEPTED SOLUTION

what version of the usergrid client library are you using?

The v0.10.07 should have this behavior:

Suppose an entity exists with the same name as the one you are saving; if you then call createEntity()...

  • with getOnExist = true, then it actually saves the thing you passed in, over the existing entity.
  • with getOnExist = false, then it will not save, but it will return the existing entity.

I find this behavior confusing to describe and use, and not consistent with the documentation you cited.

This has changed in later versions of the nodejs usergrid sdk.

If you want to continue to use the v0.10.07 version of the SDK you can monkeypatch it by adding in your own method that exhibits the behavior you want. Something like this would always save an entity, regardless whether it exists or not prior to the attempt to save:

 Usergrid.Client.prototype.createNewEntity = function (options, callback) {
    var entity = new Usergrid.Entity({
      client:this,
      data:options
    });
    entity.save(function(e, data) {
      if (typeof(callback) === 'function') {
        callback(e, entity, data);
      }
    });
  };<br>

This could lead to unintended overwrites. If you wanted to check for an existing entity first, to avoid that, then... you would need to do a fetch and process the results. something like this:

  Usergrid.Client.prototype.createUniqueEntity = function (options, callback) {
    var entity = new Usergrid.Entity({
      client:this,
      data:options
    });
    entity.fetch(function(e, data) {
      if ( ! e) {
        // no error means the fetch succeeded, which ... is an error.
        if (typeof(callback) === 'function') {
          callback(new Error('entity already exists'), entity, data);
        }
        return;
      }
      entity.save(function(e, data) {
          if (typeof(callback) === 'function') {
            callback(e, entity, data);
          }
        });
    });
  };
<br>

But I'd advise you to move to the current usergrid library.

It's a different programming model, but it's cleaner and works better.

See https://www.npmjs.com/package/usergrid

View solution in original post

4 REPLIES 4