x-ray: [ {} ] does not work

{
     addresses: [
           {
               address: '.address',
               location: '.location'
          }
     ]
}

Returns:

{ addresses: [] }

Expected:

{
     addresses: [
           {
               address: 'address',
               location: 'location'
          }
     ]
}

About this issue

  • Original URL
  • State: open
  • Created 9 years ago
  • Comments: 30 (8 by maintainers)

Most upvoted comments

this is my html

`<div class="addresses"> a b c d

</div>`

``

and i want this array

[{title:a},{title:b},{title:c},{title:d}]

what can i do?

Here’s what I’m trying to do:

<!-- example.com/index.html -->
<div id="list">
  <div class="company">
    <a class="url" href="./company1.html">Company1</a>
  </div>
  <div class="company">
    <a class="url" href="./company2.html">Company2</a>
  </div>
</div>
<!-- example.com/company1.html -->
<div id="profile">
  <span class="domain">company1.com</span>
</div>
<!-- example.com/company2.html -->
<div id="profile">
  <span class="domain">company2.com</span>
</div>
x("example.com/index.html",
  {
    companies: x('.company', [
      {
        name: 'a.url',
        domain: x('.domain', 'a.url@href')
      }
    ])
  }
)(function(err, object){
  console.log("done", err, object);
});

returns the following:

{
  companies: [
    {
      name: "Company1"
    },
    {
      name: "Company2"
    }
  ]
}

note the lack of a domain attribute on the nested objects.

If I swap the arguments for the domain lookup to

domain: xray('a.url@href', '.domain')

there is no result logged to the console. It does seem to do something, but nothing is returned to the callback function.

@matthewmueller yes that works! I guess I hadn’t tried that specific combination of nesting the x() call.

However I’m trying to do something along the following lines, and xray seems to just die on me:

x('http://example.com',
  {
    companies: x('.vcard', [
      {
        name: 'a.url',
        domain: x('a.url@href', '.domain')
      }
    ])
  }
)(function(err, obj){
  if (err) {
    console.error(err);
  }
  console.log("RESULT:", object);
});

So the first page read in by xray contains a list of links, and I’d like xray to follow each link to grab the .domain element on the resulting page.

Should this work?

When I run the above, I see no console output at all but if I remove the domain: key, the correct array of objects with name attributes is returned.