Apigee BaaS:使用usergrid创build多个实体node.js sdk不起作用

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

[ { "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'); } }); } }); 

这没有帮助同时创build多个实体。 方法createCollection创build一个集合,而不一定是一堆实体。 任何人都可以帮我这个吗?

或者我应该继续使用请求,并在Apigee BaaS上发起一个HTTPpost? 在这种情况下,我不会使用SDK。

你是正确的,现有的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); } }); };