Cheerio - Unable to obtain children of element - javascript

This is the output when printing my ${element}:
I wish to obtain the data of the first child of my ${element} that has a type: "text" and data valid duration (hours:minutes:seconds). In my case I want to obtain "5:01".
However, when trying to obtain all the children using ${element}.children() I get the following:
Basically there is no data and no type which I can check/obtain. What am I doing wrong?

From the log image I can see children property is an array not a function.
Following Code should get you the desired data.
element.children.find(child => child.type == 'text').data;

${element}.children().first().data()

Related

Postman - extracting value from HTML description list

I've searched for a while trying to find a way in Postman to extract a HTML description list value from the response body where the description list contains multiple values.
Example of response body:
<dl>
<dd>Fruit</dd>
<dt>Apple</dt>
<dd>Vegetable</dd>
<dt>Carrot</dt>
</dl>
How do I just get just the Vegetable value? I've tried using the following
const $ = cheerio.load(pm.response.text())
console.log('Vegetable', $('dt').text())
This then returns both values
"Vegetable" "AppleCarrot"
The Fruit & Vegetable values will change once the request is rerun, this means I'm unable to go just based off their names.
I'm probably over thinking this, thanks in advance.
EricG posted the following above.
JQuery allows you to filter based off order using the following:
const $ = cheerio.load(pm.response.text())
console.log('Vegetable', $('dt').first().text())
Alternatively to the above if you need to go further down the list you can use:
const $ = cheerio.load(pm.response.text())
console.log('Vegetable', $('dt').eq(0).text())
Changing the value in .eq(#) starting from 0 will then following the items down the list.

Read data from JSON with multiple JSON objects in order to be displayed

I have the following JSON object that contains multiple JSON objects.
I want to read and display the following elements 1) CampaignName 2) Start date 3) End date
I have written the following the following code which produces error to the browser console. So, the items are not displayed.
the produced error at the browser console is:
I assume I do not access the elements as appropriate.
How shall I do it ?
You are using the .each function that iterate over each object... each object has the properties that you need. Each of these values you are iterating over is an object and not an array
Use v.Campaign.CampaignName instead of accessing the index first
the v that you are trying to access already is the date on a single JSON object so there is no need of passing the count param. I would suggest you refactoring your code to something like:
data.forEach(({ Campaign: {CampaignName, StartDate, EndDate } }) => {
events.push({
title: CampaignName,
title: moment(StartDate),
title: moment(EndDate),
})
})

Axios how to get data inside of square brackets?

So I'm new with Axios and I'm trying to make a Discord bot that takes info from an api website and uses it. The issue is the data given looks like:
{"status":1,"size":1,"result":[{"number":"9999", and so on.
How do I get the data inside of the [ ]? I tried:
var teamNumber = response.data.result.number
but it doesn't work.
I can get response.data.status, but not result.number
TLDR: how to get 9999 from {"result":[{"number":"9999", ?
result is an array; if you want to get the first element from it (and get that element's "number" field) you'd do:
console.log(response.data.result[0].number);
If you want to loop over them you'd do:
response.data.result.map(el => {
console.log(el.number);
});

Getting a date to pull from a parent to a response on creation

So given this code for pre loading fields from a parent record to a new response record. I can get the ParentUNID and the HolderName values to load no problem, but I am missing something on passing the date values?
<xp:this.beforePageLoad><![CDATA[#{javascript:
var parentDoc = database.getDocumentByUNID(document1.getParentId());
document1.setValue("ParentUNID", parentDoc.getItemValue("DocID"));
document1.setValue("Policy_HolderName", parentDoc.getItemValue("Policy_HolderName"));
document1.setValue("Policy_Eff_Date", parentDoc.getItemValue("Policy_Eff_Date"));}]]></xp:this.beforePageLoad>
Copy the item itself and not just item's value. This way you copy all item's properties too:
...
document1.getDocument().copyItem(parentDoc.getFirstItem("Policy_Eff_Date"));
...

Retrieve JSON Array element value

My web service returned a JSON Array (ie. [{"key":"value"}, {"key":"value2"}]). In the array there are two items as you can see, which are separated with comma. I want to know how can I access the second item, and get the value of "key" for the second item.
I've tried:
var a = msg.d[1].key
With no success of course.
This is the returned string:
"[{"Code":"000000","Name":"Black","Id":9},{"Code":"BF2C2C","Name":"Red","Id":11}]"
The string was extracted using FireBug after watching the msg.d.
Need your help in solving this.
msg[1].key
Assuming that the name of that array is msg. I'm not sure what you are using .d for.
If msg.d is a string representing an array, use JSON.parse.
JSON.parse(msg.d)[1].key
You can replace key with the key you are wanting, e.g. Code, Name, Id, etc.
This works as expected for me.
var msg = [{"key":"value"}, {"key":"value2"}];
var a = msg[1].key;
What is msg in the example above? Need more info to help.
If msg.d is a string then you have to eval (uggh) or parse it before applying the array subscript.

Categories