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 227
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

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

@Dino @Anil Sagar @Scott GanyoOk, I can use the new version(0.10.11). I am actually build nodejs in Apigee Edge. So can I use a package.json in this way,

{
    "name":"usergrid-node",
    "version":"0.0.0",
    "description":"I am using this json to",
    "main":"main.js",
    "dependencies": {
        "express":"3.x.x",
        		"usergrid":"0.10.11",
        "argo":"x.x.x"
    }
}

Will this be enough for me to use the current usergrid library?

-edit-

I hope this is what I have to do?

https://community.apigee.com/questions/4319/nodejs-install-module.html

..

Hi guys few days back I update my usergrid module in my proxy to usergrid@0.10.11. But till yesterday getOnExist:true did not throw any error even if a duplicate entry was made. But today when I gave an duplicate entry it started throwing the error.

It is good that it works now, but I am confused why it worked today and not earlier? Any ideas? @Dino @Anil Sagar


I don't know, but I think....

if you have a new question you should ask it as a new question, rather than posting it as an answer to an existing question.