【问题标题】:Apigee BaaS: Create multiple entities using usergrid node.js sdk doesn't workApigee BaaS:使用 usergrid node.js sdk 创建多个实体不起作用
【发布时间】:2014-06-20 17:26:45
【问题描述】:

我正在使用 Apigee BaaS。从 UI 即 apigee.com/appservices,我可以使用 JSON 数组同时创建多个实体。例如这里的 json 数组在集合 /employees 上创建了三个实体。

    [
        {
            "name": "John",
            "employeeId": "1"
        },
        {
            "name": "Doe",
            "employeeId": "2"
        },
        {
            "name": "Danny",
            "employeeId": "3"
        }
    ]

现在我正在尝试使用 nodeJs SDK 模拟相同的功能 - https://github.com/usergrid/usergrid/tree/master/sdks/nodejs

        client.createEntity(options, function(err, entity) {
            if (err) {
                //error - entity not created
            } else {
                    //set is additive, so previously set properties are not overwritten
                entity.set(entityJsonArr);

                //finally, call save on the object to save it back to the database
                entity.save(function(err) {
                    if (err) {
                        //error - entity not saved
                        console.log('failure');
                    } else {
                        //success - new entity is saved
                        console.log('success');
                    }
                });
            }
        });

这无助于同时创建多个实体。方法 createCollection 创建一个集合而不一定是一堆实体。谁能帮我解决这个问题?

或者我应该继续使用请求并在 Apigee BaaS 上触发 HTTP 帖子吗?在那种情况下,我不会使用 sdk。

【问题讨论】:

    标签: node.js apigee


    【解决方案1】:

    您是对的,现有的 Node SDK 无法处理这种情况。如果你想要并且它对你有用,我现在使用以下猴子补丁来解决这个问题:

    var Usergrid = require('usergrid');
    
    Usergrid.client.prototype.batchCreate = function (type, entities, callback) {
      if (!entities.length) { callback(); }
    
      var data = _.map(entities, function(entity) {
        var data = (entity instanceof Usergrid.entity) ? entity.get() : entity;
        return _.omit(data, 'metadata', 'created', 'modified', 'type', 'activated');
      });
    
      var options =  {
        method: 'POST',
        endpoint: type,
        body: data
      };
    
      var self = this;
      this.request(options, function (err, data) {
        if (err && self.logging) {
          console.log('could not save entities');
          if (typeof(callback) === 'function') { callback(err, data); }
          return;
        }
    
        var entities = _.map(data.entities, function(data) {
          var options = {
            type: type,
            client: self,
            uuid: data.uuid,
            data: data || {}
          };
          var entity = new Usergrid.entity(options);
          entity._json = JSON.stringify(options.data, null, 2);
          return entity;
        });
    
        if (typeof(callback) === 'function') {
          return callback(err, entities);
        }
      });
    };
    

    【讨论】:

    • 谢谢你!我什么时候可以在 sdk 中看到这个?
    • 对不起。我不确定。
    • 效果很好!谢谢,斯科特
    猜你喜欢
    • 1970-01-01
    • 2015-05-01
    • 2014-11-22
    • 2014-07-03
    • 2016-07-08
    • 2023-03-10
    • 2014-12-10
    • 2015-01-09
    • 1970-01-01
    相关资源
    最近更新 更多