I have function node to read data from JSON file, I want read-only last value in the array. I have written the following code in function node but shown error " "TypeError: Cannot read property 'length' of undefined"``
function node code is
var array = [];
for (var i=0; i<msg.payload.value.length;i++) {
array.push({payload: msg.payload.value[i]});
}
return array;
the following output for json node that I want read last value of it by using the code above
Thank for your reply,
I have tried json node but still the same error. I could not access to last value in the last object, please could you guide me ?
after covert it to josn object still the same problem as shown below
Thanks, Sir for your nice reply, it returns the last document but what I want exactly serach through all document and display only the document that has the current date (based on timestamp), please cloud you help me with !!
It could be done with a simple iteration but it is a little bit difficult since I do not have a dataset for testing.
Try to use this code inside a function node and please show me the result that will appear in your debug panel. This will tell me I got right the structure of your dataset.
function extract(elem) {
let extr = elem.payload.timestamp;
node.warn(extr)
return extr;
}
msg.payload.forEach(extract);
return msg;
I am not optimistic that this code will work since I could not test thoroughly and looks like your payload is not consistent (weird data inside the timestamp value) but lets give a try...
Try this code inside a function node
let today = new Date().toDateString();
let extractor = function (elem) {
let extr = elem.payload.timestamp.toDateString();
node.warn(extr);
return extr == today;
}
var output = msg.payload.filter(extractor);
node.warn(output);
return msg;
Explanation:
it creates a function named extractor that will return true if the timestamp in your array element equals today. Otherwise the function returns false. Every time this function returns "true" the array element will be added to the array "output"
I was expecting this error. I could only help now if you export your flow or at least the array you are trying to work out. If you share your flow don´t forget to add 3 backticks before and after otherwise it wont be possible to import and test it.
is that clear, all objects have the same data but with different times as shown below.
this flow read from DB then convert the data to JSON Object .after that the main job for function node is read-only the document that has date equal to the current date.
This flow read from DB then convert the data to JSON Object .After that the main job for function node is reading -only the documents that have the date equal to the current date.
Check if this code works inside the function node, without generating errors.
let today = new Date().toDateString();
let extractor = function (elem) {
let extr = new Date(elem.payload.timestamp).toDateString();
node.warn(extr);
return extr == today;
}
var output = msg.payload.filter(extractor);
node.warn(output);
return msg;
The code provided will only select elements of the array and display them in the debug node in yellow color (thanks to the node.warn statement). Now it is up to you to define what properties of the object you want to display. If all properties then modify the code by adding this line at the end