N3VO
N3VO

Reputation: 39

how to compare objects/strings with looping function node js?

I have been trying to find documentation on this particular subject with no luck. I have built a personal script that finds a listing and posts it to a discord web-hook, this script loops every 60 seconds, the problem I am trying to solve is that every-time it loops, it will post the same product to the web-hook, I am trying to find a way to compare them so that if its the identical item, it will not repost and rather log since new listings aren't posted that often. I've left a little bit of code out because it has private information in it, but its just a standard https request using axios. If you look at the code you can see I am trying to figure out how to compare one of the objects, but it obviously doesn't work because the titles always = themselves. Any pointing to documentation or examples would be greatly appreciated!


setInterval(function(){

axios(config)
.then(function (response) {
  let picture = (response.data.listings[0].photos[0]._links.small_crop.href);
  let link = (response.data.listings[0]._links.web.href);
  let title = (response.data.listings[0].title);
  let price = (response.data.listings[0].price.amount);
  let condition = (response.data.listings[0].condition.display_name);
  let description = (response.data.listings[0].description);
  
  if(title === title) {
    console.log("No New Pedals")
    
  }
  else{

  const msg = new webhook.MessageBuilder()
                .setImage(picture)
                .setName("🎛️ " + "New Listing Found!" + " 🎛️")
                .setTitle(title)
                .addField(price, condition + " Condition")
                .setColor("#f6870f")
                .setURL(link)


                
                
Hook.send(msg);


}})
},10000);

Upvotes: 1

Views: 64

Answers (1)

trincot
trincot

Reputation: 350270

Just use a variable that has a larger scope than the setInterval callback: you can use it to keep track of the previous value of title:

let prevTitle; // Add this variable

setInterval(function(){
    axios(config).then(function (response) {
        // ...
        if(title === prevTitle) { // Compare with it
            console.log("No New Pedals");
        } else {
            prevTitle = title; // Keep track of the change
            // ...
        }
    });
}, 10000);

Upvotes: 1

Related Questions