#If it works is it wrong

1 messages ยท Page 1 of 1 (latest)

tepid juniper
#

No it did not work. (I mean your code was great and clear. it worked) But for loop, it is outputting only once not whole loop. It only see first link image0.png...none after that. I am using this code in function node >https://pastebin.com/1yRaPcKBNo

#

I had moved " return msg;" inside the loop.
But it seems, even outside it only returns the last Image15.png

frosty peak
#

oh, okay...hang on

#

@tepid juniper Where is the list of files coming from?

#

or rather...are these being auto-generated?

#

Understanding what controls how many URLs are going to be created will be helpful.

tepid juniper
# frosty peak or rather...are these being auto-generated?

Actually its a website ..it autouploads everyday (PDF than png).
Number of files is 1 to 16 1.pdf, 2.pdf, 16.pdf . Like that

I need to feed these to a telegram group, where they are fetched. That feeding part etc is all working in nodered

#

I am just crawling everyday

frosty peak
#

Okay, so the file count isn't necessarily consistent.

#

Can you show me the code that feeds the node we're working on?

tepid juniper
frosty peak
#

Hmm, this could be useful for me. I live on an island so I'm beholden to a ferry provider and been thinking about how to scrape their schedules since they don't provide API access.

tepid juniper
frosty peak
#

Yeah, and I know why it's only sending one link, probably the last link actually.

#

Because you need to build an object, right now we're just building msg.link and then overwriting it until the counter limit is reached

#

and then when that limit is reached, the loop exits and msg.link has the last definition stored in it

#

make sense?

tepid juniper
frosty peak
#

Okay, so the cron node scrapes the page and harvests the links, and that's the inbound msg.payload we're working with?

tepid juniper
frosty peak
#

yeah, return "returns" out of a function, so wherever you put it, that's the "get out of here" function.

tepid juniper
# frosty peak make sense?

yes makes sense... thats what i am wondering...how will the node inject all for loop iterations to next node?

frosty peak
#

We need to build an object

#

So I'm thinking we need to know the number of files we're going to produce ahead of time, then we can build an array with the links in it and that will be an object inside the msg.payload we return.

tepid juniper
frosty peak
#

yeah, let me think on it...It's 9:30am where I live, so I am now juggling work with recreation. ๐Ÿ˜‰

#

super nerdy recreation, but recreation nonetheless.

tepid juniper
#

Ok no problem. take your time. Sorry for bothering. meanwhile i will search more

tepid juniper
#

Pedro helped here to push as array, which i can use in future nodes
#general-archived message

Even though i still could not find how to push each from function node itself, but it might work

frosty peak
#

Here you go:

var BaseURL = "http://www.website.com/";
var today = new Date();
var year = today.getFullYear();
var yr = today.getFullYear().toString().slice(-2);
var months = { 1: "jan", 2: "feb", 3: "mar", 4: "apr", 5: "may", 6: "jun", 7: "jul", 8: "aug", 9: "sep", 10: "oct", 11: "nov", 12: "dec" };
var month = today.getMonth();
var monthName = months[month];
var date = today.getDate();
var file = "Image"
var ext = ".png"
var link = [""];
for (let i = 0; i < len; i++) {
    if (i < 10) { 
        var x = "0" + i.toString()
        } else {
            var x = i.toString()
        }
    link[i] = BaseURL + year + "/" + date + monthName + yr + "/" + file + x + ext;
}
msg.payload = link;
return msg;```
#

I added a tweak to pad the number in the file name with a 0 if it's less than 10, that way the files will sort properly.

tepid juniper
#

Thanks i will look at your flow too. to learn a thing more ๐Ÿ™‚

tepid juniper
#
  1. I just noticed that the Month Variable is giving output of 1 month less. Current month Aug. It tells july Array count problem? array starts from 0 i think

FIXED : var months = { 0: "jan", 1: "feb", 2: "mar", 3: "apr", 4: "may", 5: "jun", 6: "jul", 7: "aug", 8: "sep", 9: "oct", 10: "nov", 11: "dec" };

  1. Our output for date is http://website/2022/5aug22/1.png
    But website seems to write date in 2 digits and puts 0 before single digit dates like http://website/2022/05aug22/1.png
    Does it have an easy fix? Sorry if i am asking too much ๐Ÿ™‚
frosty peak
#
var BaseURL = "http://www.website.com/";
var today = new Date();
var year = today.getFullYear();
var yr = today.getFullYear().toString().slice(-2);
var months = { 0: "jan", 1: "feb", 2: "mar", 3: "apr", 4: "may", 5: "jun", 6: "jul", 7: "aug", 8: "sep", 9: "oct", 10: "nov", 11: "dec" };
var month = today.getMonth();
var monthName = months[month];
var date = today.getDate();
if (date < 10) {
    var strDate = "0" + date.toString()
} else {
    var strDate = date.toString()
}
var file = "Image"
var ext = ".png"
var link = [""];
for (let i = 0; i < len; i++) {
    if (i < 10) { 
        var x = "0" + i.toString()
        } else {
            var x = i.toString()
        }
    link[i] = BaseURL + year + "/" + strDate + monthName + yr + "/" + file + x + ext;
}
msg.payload = link;
return msg;```
tepid juniper
#

Thanks. works ๐Ÿ™‚ ๐Ÿ‘๐Ÿฝ

spring summit
#

jumping in here for a moment. I'm noticing variables turning around and defining other variables. such as year and then yr. get rid of the yr variable, and then change the line towards the end to javascript link[i] = BaseURL + year + "/" + strDate + monthName + year.toString().slice(-2) + "/" + file + x + ext;

#

same with month and monthName. Also, file and ext. They're just extra lines or can be incorporated into the link string.
Perhaps something more like:

link[i] = BaseURL + today.getFullYear() + "/" + strDate + monthName + today.getFullYear().toString().slice(-2) + "/Image" + x + ".PNG";
#

the IF i < 10 sections, might be able to be simplied with a RegEx statement but I'd have to play around to try to figure that one out.

#

months, technically, could be simplified into an array can just reading the string based on the key. so something like var months = ["jan", "feb", "mar"...] or something like that. even better, could be letting the code derive the name without extra lines. Some examples can be found here https://stackoverflow.com/questions/1643320/get-month-name-from-date

#

consider using something like this for the 0 padding in the strDate: const time_now = time_now_hours + ":" + ('00' + time_now_minutes).slice(-2); (This is line from one of my functions and is used to ensure a 0 before a single digit minute such as 5.)

tepid juniper
#

Thanks for the info Derek. ๐Ÿ‘๐Ÿฝ I kinda like the noob code earlier (the way i am). But i appreciate your input (slowly i will become a pro lol)

frosty peak
frosty peak
spring summit
# frosty peak Is defining variables from other variables considered bad form?

I wouldn't necessarily say that it is bad form. Generally, it's my understanding that a value should be defined as a variable if it is going to be referenced two or more times within a piece of code. So yr, for example, is only referenced once. Additionally, it is a modified version of the year variable. Defining variables also allocates memory in order to store that variable; not necessarily a big deal now-a-days but something to consider. I think, however, defining variables that can act as "settings" for your code should be defined, such as if the file and ext variables were used in such a way that the script could quickly be modified to serve a different function like Webpage and .html, respectively. To me, really, the less lines in a code makes it easier to ready and with my ideas (or v2.0 suggestions) the number of lines the above function could have would be cut in half.