Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7543437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:25:42+00:00 2026-05-30T08:25:42+00:00

I’m trying to scrape data from an external JSON feed and store it in

  • 0

I’m trying to scrape data from an external JSON feed and store it in my mongoDB.

request(url, function (error, response, body) {
    if (!error && response.statusCode == 200) {
        var jsonObj = JSON.parse(body);
        // console.log(jsonObj.events[1].id) <-- this works
        for (var i = 0; i < jsonObj.events.length; i++) { 
            // add jsonObj.events[i] as a new record to table
            // console.log(jsonObj.events[i].id) <-- this works
            Wnet.findOne({ id : jsonObj.events[i].id }, function (err, doc){
                if (err || doc == null) {
                     // console.log(jsonObj.events[i].id) <-- this doesn't work!
                     // record is new, add it
                }
            });
         }
     }
});

Notice the console.log statements… the last one (inside the .findOne‘s callback) doesn’t work.

I’m still learning the ins and outs of JavaScript, but I believe this is a scope issue… the callback function doesn’t know jsonObj. But, I don’t know how to pass it into the function.

Taking a step back, I’m trying to check the collection for existence of the record. If it can’t find it, I want to add the record from the fetched JSON array. If there’s a better way to do that, I’d love your help in finding it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:25:44+00:00Added an answer on May 30, 2026 at 8:25 am

    This is not a scope issue. In terms of scope, it will look for the variable by bubbling up. So in the findOne it has the variable jsonObj. It will bubble up to the var jsonObj = ... above.

    I believe your issue is that you are looping through jsonObj.events and firing off findOne for each. You are then trying to console jsonObj.events[i].id in the findOne callback for each. The problem is that your i is incremented each time. And will actually end when i >= jsonObj.events.length. So if the length is 9 then i will end up being 10 when the loop exits. So your logging will not work. EDIT: To clarify, the logging will not work because within the loop all the findOne will fire off but the callbacks won’t happen right away. Because the callbacks are async. So by the time the callbacks happen the loop is likely finished, and it will try to log an events that does not exist.

    Try this:

    request(url, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            var jsonObj = JSON.parse(body);
            //console.log(jsonObj.events[1].id) <-- this works
            for (var i = 0; i < jsonObj.events.length; i++) { 
                // add jsonObj.events[i] as a new record to table
                //console.log(jsonObj.events[i].id) <-- this works
                Wnet.findOne({ id : jsonObj.events[i].id }, function (err, doc){
                    if (err || doc == null) {
                        console.log(jsonObj.events[this].id);
                        //record is new, add it
                    }
                }.bind(i));
             }
         }
    });
    

    This is just one way of dealing with it. If the bind is confusing then try a different way. You could also do something like:

        request(url, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            var jsonObj = JSON.parse(body);
            //console.log(jsonObj.events[1].id) <-- this works
            for (var i = 0; i < jsonObj.events.length; i++) { 
                // add jsonObj.events[i] as a new record to table
                //console.log(jsonObj.events[i].id) <-- this works
                doFindOne(jsonObj.events, i);
             }
         }
    });
    
    
    function doFindOne(events, i) {
        Wnet.findOne({ id : events[i].id }, function (err, doc){
            if (err || doc == null) {
                console.log(events[i].id);
                //record is new, add it
            }
        });
    }
    

    In this second example we are calling a function which will keep the i in scope to that call. This may make more sense. (binding tends to confuse more than clarify)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to construct a data frame in an Rcpp function, but when I
I am trying to render a haml file in a javascript response like so:
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.