How to read a Entity reference node's fields? I had a field using entity reference in my site. I'm trying to develop a module can read/use it.

$wrapper = entity_metadata_wrapper('node',$node);
dpm($wrapper-&gt;myRefField-&gt;getPropertyInfo());


This returns a error page, but dpm($wrapper->myRefField) shows that it's a standard EntityDrupalWrapper, though nothing in it. 

How can I read $nid of the referenced node?

Errors below:

 Additional uncaught exception thrown while handling exception.

Original

PDOException: SQLSTATE[42P01]: Undefined table: 7 ERROR: relation &amp;quot;node&amp;quot; does not exist LINE 3: node base ^: SELECT revision.vid AS vid, base.uid AS uid, revision.title AS title, revision.log AS log, revision.status AS status, revision.comment AS comment, revision.promote AS promote, revision.sticky AS sticky, base.nid AS nid, base.type AS type, base.language AS language, base.created AS created, base.changed AS changed, base.tnid AS tnid, base.translate AS translate, revision.timestamp AS revision_timestamp, revision.uid AS revision_uid FROM {node} base INNER JOIN {node_revision} revision ON revision.vid = base.vid WHERE (base.nid IN (:db_condition_placeholder_0)) ; Array ( [:db_condition_placeholder_0] =&amp;gt; 1 ) in DrupalDefaultEntityController-&gt;load() (line 191 of /var/www/site2/includes/entity.inc).

Additional

PDOException: SQLSTATE[42P01]: Undefined table: 7 ERROR: relation &amp;quot;filter_format&amp;quot; does not exist LINE 3: filter_format ff ^: SELECT ff.* FROM {filter_format} ff WHERE (status = :db_condition_placeholder_0) ORDER BY weight ASC; Array ( [:db_condition_placeholder_0] =&amp;gt; 1 ) in filter_formats() (line 427 of /var/www/site2/modules/filter/filter.module).

Uncaught exception thrown in shutdown function.

PDOException: SQLSTATE[42P01]: Undefined table: 7 ERROR: relation &amp;quot;semaphore&amp;quot; does not exist LINE 1: DELETE FROM semaphore ^: DELETE FROM {semaphore} WHERE (value = :db_condition_placeholder_0) ; Array ( [:db_condition_placeholder_0] =&amp;gt; 18701629495353e89467a483.34692095 ) in lock_release_all() (line 269 of /var/www/site2/includes/lock.inc).

Uncaught exception thrown in session handler.

PDOException: SQLSTATE[42P01]: Undefined table: 7 ERROR: relation &amp;quot;sessions&amp;quot; does not exist LINE 3: sessions sessions ^: SELECT 1 AS expression FROM {sessions} sessions WHERE ( (sid = :db_condition_placeholder_0) AND (ssid = :db_condition_placeholder_1) ); Array ( [:db_condition_placeholder_0] =&amp;gt; sLS3fA1YAd1u1G6_xC1yxg0Ba6Kgrus6oXDBm4Wpuww [:db_condition_placeholder_1] =&amp;gt; ) in _drupal_session_write() (line 209 of /var/www/site2/includes/session.inc).
&lt;?php

/**
 * We define a new function in our MODULE and will simply use it where needed.
 *
 */

/**
 * @param (object) $node
 * @param (string) $field_name
 * @return false|null|integer
 *    The target id. FALSE on error. NULL when no reference found.
 */
function _MODULE_get_node_reference($node, $field_name) {
    $target_id = null;
    if (!isset($node-&gt;type)) {
        return false;
    }
    // In case of any exception we simply return false.
    try {
        $entity_wrapper = entity_metadata_wrapper('node', $node);
        if (isset($entity_wrapper-&gt;{$field_name})) {
            $target_id = $entity_wrapper-&gt;{$field_name}-&gt;value();
        }
        // maybe we are in the middle of a DB transaction. In that case entity_metadata_wrapper() would not work
        if (empty($target_id)) {
            // So, we try using Fields API. This always works.
            $items = field_get_items('node', $node, $field_name);
            if (isset($items[0])) {
                $target_id = $items[0]['target_id'];
            }
        }
    } catch (Exception $e) {
        return false;
    }
    return $target_id;
}

?&gt;