src/back/MongoAdapter.js
function getObject(EntityClass, query) {
  expect(arguments).to.have.length(
    2,
    'Invalid arguments length when inserting an object in a MongoAdapter ' +
    '(it has to be passed 2 arguments)'
  );

  var cursor;
  var document;

  function findDocument(db) {
    cursor = _buildCursor(db, EntityClass, query);
    return cursor.next();
  }

  function checkNotEmpty(doc) {
    // save document
    document = doc;

    // check for no result
    if (doc === null) {
      throw new QueryError('Object does not exist');
    }
  }

  function checkNotMultiple() {
    // check for multiple results
    return cursor.hasNext()
      .then(function (hasNext) {
        if (hasNext) {
          throw new QueryError('Query matches multiple objects');
        }
      });
  }

  function populateEntity() {
    // return populated entity
    return documentToObject(document, EntityClass.adapterName);
  }

  return this.getDatabase()
    .then(findDocument)
    .then(checkNotEmpty)
    .then(checkNotMultiple)
    .then(populateEntity);
}
function getObject(EntityClass, query) {
  expect(arguments).to.have.length(
    2,
    'Invalid arguments length when inserting an object in a MongoAdapter ' +
    '(it has to be passed 2 arguments)'
  );

  var cursor;
  var document;

  function findDocument(db) {
    cursor = _buildCursor(db, EntityClass, query);
    return cursor.next();
  }

  function checkNotEmpty(doc) {
    // save document
    document = doc;

    // check for no result
    if (doc === null) {
      throw new QueryError('Object does not exist');
    }
  }

  function checkNotMultiple() {
    // check for multiple results
    return cursor.hasNext()
      .then(function (hasNext) {
        if (hasNext) {
          throw new QueryError('Query matches multiple objects');
        }
      });
  }

  function populateEntity() {
    // return populated entity
    return documentToObject(document, EntityClass.adapterName);
  }

  return this.getDatabase()
    .then(findDocument)
    .then(checkNotEmpty)
    .then(checkNotMultiple)
    .then(populateEntity);
}
function getObject ( EntityClass , query ) { expect ( arguments ) . to . have . length ( 2 , 'Invalid arguments length when inserting an object in a MongoAdapter ' + '(it has to be passed 2 arguments)' ) ; var cursor ; var document ; function findDocument ( db ) { cursor = _buildCursor ( db , EntityClass , query ) ; return cursor . next ( ) ; } function checkNotEmpty ( doc ) { // save document document = doc ; // check for no result if ( doc === null ) { throw new QueryError ( 'Object does not exist' ) ; } } function checkNotMultiple ( ) { // check for multiple results return cursor . hasNext ( ) . then ( function ( hasNext ) { if ( hasNext ) { throw new QueryError ( 'Query matches multiple objects' ) ; } } ) ; } function populateEntity ( ) { // return populated entity return documentToObject ( document , EntityClass . adapterName ) ; } return this . getDatabase ( ) . then ( findDocument ) . then ( checkNotEmpty ) . then ( checkNotMultiple ) . then ( populateEntity ) ; }
Get object from the database matching given query.
@name module:back4app-entity-mongodb.MongoAdapter#getObject
@function
@param {!module:back4app-entity/models.Entity} entityObject The Entity Class
which instance will be searched within the collections.
@param {?Object} query Object for query search.
@returns {Promise.<object|Error>} Promise that returns found object if
succeed or Error if failed.
@example
mongoAdapter.getObject(Car, {color: 'red'})
.then(function(car) {
console.log(car);
});
Get object from the database matching given query .