如何将此功能合并在一起

时间:2017-05-03 21:26:37

标签: javascript angularjs arrays node.js express

现在,当我运行/sitemap.xml

时,我有了这段代码来创建sitemap.xml
 database = firebase.database();
    var ref = database.ref('urls');
    ref.on('value', gotData, errData);

    function errData(err){
        console.log('Error!');
        console.log(err);
    }  


function gotData(data){
        result = data.val() 
        return urls = Object.keys(result)
                     .filter(key => result[key].last_res > 5)
                     .map(key => ({url: '/' + result[key].url_site + '/'}))
    }

当我尝试在gotData(data)函数中运行console.log(urls)时,它返回为

{ salmon: 
   { count: 1,
     last_res: 10,
     url_site: 'salmon' },
  'salmon-food': 
   { count: 1,
     last_res: 601,
     url_site: 'salmon-food' } }

我需要将此代码合并到上面的函数

var sitemap = sm.createSitemap({ 
            hostname: 'xxx.com',
            cacheTime: 600000,
            urls: urls

        });
app.get('/sitemap.xml', function(req, res) {
    sitemap.toXML( function (err, xml) {
        if (err) {
            return res.status(500).end();
        }
        res.header('Content-Type', 'application/xml');
        res.send( xml );
    });
    });
}

我需要在gotData(data)中返回url来创建sitemap.xml。那么如何将这些代码合并在一起

1 个答案:

答案 0 :(得分:0)

错过了许多代码,但常见的方法是:

function gotData (data) {
    const result = data.val();
    const urls = Object.keys(result)
        .filter(key => result[key].last_res > 5)
        .map(key => ({url: 's/price/' + result[key].url_site + '/'}));

    const sitemap = sm.createSitemap({     // supposing it is sync...
        hostname: 'xxx.com',
        cacheTime: 600000,
        urls
    });

    return sitemap;
}

app.get('/sitemap.xml', (req, res) => {
    const siteMap = gotData(data???);
    sitemap.toXML(siteMap, (err, xml) => {
        if (err) return res.status(500).end();

        res.header('Content-Type', 'application/xml');
        return res.send( xml );
    });
});