Hi y’all, Carolina from support here!!
If you’re fetching data from a TagoIO device, you’ll receive an array of data records (each with variable, value, time, id, etc.). Instead of writing manual loops to locate a specific item (e.g., the last temperature or a record by group), use Array.find() for cleaner, faster, and more readable code.
This is how it works…TagoIO returns device data as an array of objects with fields like variable, value, time, id, unit, group, etc.
When using the Device class (device.getData), you also receive an array you can directly inspect.
Example (Device class with device.getData) Suppose you pulled the latest 50 records and want the first temperature item without looping:
/ Using the Device class (device token)
import { Device } from "@tago-io/sdk";
const device = new Device({ token: "your-device-token" });
async function getTemperature() {
try {
// Retrieve recent data (you can filter variables to reduce payload)
const data = await device.getData({
variables: ["temperature", "humidity"],
qty: 50, // default is 15, increase as needed
ordination: "descending",
});
// Find the first 'temperature' item
const temperatureItem = data.find((item) => item.variable === "temperature");
if (!temperatureItem) {
console.log("No temperature data found.");
return;
}
console.log("Temperature:", temperatureItem.value, temperatureItem.unit, "at", temperatureItem.time);
} catch (error) {
console.error("Error getting device data:", error);
}
}
getTemperature();
Why Array.find() over loops?
-
Simpler and more expressive: reads like your intent (find the first match).
-
Stops scanning at the first match, unlike filter or manual for loops that often keep iterating.
-
Reduces code complexity and improves maintainability.
Has this information helped you? Have any questions or concerns about this? Share with me, I will be more than happy to help you!
Happy week,