0

I've been working on an application for a while and this particular feature is a part of a function which is supposed to read data from an api into an array so that I can display the contents on a webpage. Right now, I'm stuck here. Originally, I had a much longer section of code that wasn't working, but I've cut it down to a more specific problem: jsonBody.length returns 5, as expected, but articles.length returns 'undefined' and I don't understand why.

request(options, function(err, request, body) {
    var jsonBody = JSON.parse(body);
    var articles = new Article(jsonBody);

    console.log(jsonBody.length);
    console.log(articles.length);

    res.render('news');
});

Thank you so much if you could help me understand. I'm not even entirely sure that I'm supposed to be using var articles at all? I can get the JSON to print to the console just find if I use jsonBody, but if I do so, I'm not sure how to utilize the contents on my 'news' page.

Here is the extended code in case you would like to see.

var marketNewsSchema = new mongoose.Schema({
    datetime: String,
    headline: String,
    source: String,
    url: String,
    summary: String,
    related: String,
    Image: String
});

var Article = mongoose.model('MarketNews', marketNewsSchema);

app.get('/api/marketNews', function(req,  res) {
    var query = {
        'symbol': req.body.id
    };

    var options = {
        url: 'https://api.iextrading.com/1.0/stock/aapl/news/last/5',
        method: 'GET',
        qs: query
    };

    request(options, function(err, request, body) {
        var jsonBody = JSON.parse(body);
        var articles = new Article(jsonBody);

        console.log(jsonBody.length);
        console.log(articles.length);

        res.render('news');
    });
});

and the raw JSON object should be in this format:

[
    {
        "datetime": "2017-06-29T13:14:22-04:00",
        "headline": "Voice Search Technology Creates A New Paradigm For Marketers",
        "source": "Benzinga via QuoteMedia",
        "url": "https://api.iextrading.com/1.0/stock/aapl/article/8348646549980454",
        "summary": "<p>Voice search is likely to grow by leap and bounds, with technological advancements leading to better adoption and fueling the growth cycle, according to Lindsay Boyajian, <a href=\"http://loupventures.com/how-the-future-of-voice-search-affects-marketers-today/\">a guest contributor at Loup Ventu...",
        "related": "AAPL,AMZN,GOOG,GOOGL,MSFT",
        "image": "https://api.iextrading.com/1.0/stock/aapl/news-image/7594023985414148"
    }
]
1
  • 1
    What happens when you output console.log(articles[0].length);? Commented Nov 30, 2018 at 9:46

1 Answer 1

3

I think your problem is that new Article() is not an array, but you expect it to be one.

As far as I can see, Article is a mongoose schema - not an array.

So if your jsonBody is an array of articles, you might want to map over this array and generate individual articles for each object in the list.

I.e.:

  var jsonBody = JSON.parse(body);
  var articles = jsonBody.map(function(data) { 
     return new Article(data);
  })

  console.log(articles.length);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.