简体   繁体   中英

Saving Yummly API Module Output to Array in Node.JS Instead of Console.Log

I'm just starting out with javascript and node.js, so this is probably basic. What I want to do is save the output from the Yummly query into a variable. Preferably an array or a list. Eventually a dictionary, but right now I just need to make headway on the basic concept and I can figure the rest out.

The module works and the data outputs to the console correctly, but I can't get it to save to any sort of variable. I have tried both push and concat in just about very location a program of this limited size format allows.

Can someone please explain or demonstrate how to save the output of the Yummly query to an array or list instead of to the console?

If possible, could you also explain why it doesn't work as it is written now? With names a global new global array and each recipe name being pushed to it in the inner loop?

PS I'm primarily a Python programmer trying to make the jump, so the extra information would be appreciated.

const Yummly = require('ws-yummly');
    Yummly.config({
            app_key: 'KEY GOES HERE',
            app_id: 'ID GOES HERE'
    });

    const names = new Array();
    Yummly.query('chicken')
            .maxTotalTimeInSeconds(1400)
            .maxResults(20)
            .minRating(3)
            .get()
            .then(function(resp){
                    resp.matches.forEach(function(recipe){
                            console.log(recipe.recipeName);
                            names.push(recipe.recipeName);
                    });
            });
    console.log(names);

Short story you need to wait for the query to finish. Here's a working sample.

const Yummly = require('ws-yummly');
Yummly.config({
    app_key: 'KEY GOES HERE',
    app_id: 'ID GOES HERE'
});

async function main () {
    const resp = await Yummly.query('chicken')
        .maxTotalTimeInSeconds(1400)
        .maxResults(20)
        .minRating(3)
        .get();
    const names = resp.matches.map(recipe => recipe.recipeName);
    console.log(names);
}

main().catch(error => console.error(error))

Your code is equivalent too

const Yummly = require('ws-yummly');
Yummly.config({
        app_key: 'KEY GOES HERE',
        app_id: 'ID GOES HERE'
});

const names = new Array();
console.log(names);
Yummly.query('chicken')
        .maxTotalTimeInSeconds(1400)
        .maxResults(20)
        .minRating(3)
        .get()
        .then(function(resp){
                resp.matches.forEach(function(recipe){
                        console.log(recipe.recipeName);
                        names.push(recipe.recipeName);
                });
        });

because the .then occurs after the network request.

The fix is to use async/await.

As I know, const variable cannot be used for reassigned or mutable data. Instead you can try:

let names = [];

And on the foreach loop:

names = [...names, newArrayValue];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM