Javascript 将数据异步写入承诺内的GCS
我正试图找到一种方法,将json数据写入Google云存储桶中的文件,这是一个承诺 我发现,如果我尝试和。将值一个接一个地推送到一个数组中,然后返回该值,它只会给出数组中的前3个结果,而console.log将返回所有结果 如果我尝试在本地范围内写一些东西,它只返回数组中的最后一个值,并覆盖所有以前的值,而不是附加它们 所以本质上我的问题是:有没有办法写一个承诺或类似的东西,等待所有的循环值被收集起来,一旦收集完毕,将这些值返回给一个函数,然后将其全部上传到GCS 或者,有没有一种方法可以让我在抓取数据的同时,将这些值异步写入GCS中的.json文件Javascript 将数据异步写入承诺内的GCS,javascript,node.js,google-cloud-storage,Javascript,Node.js,Google Cloud Storage,我正试图找到一种方法,将json数据写入Google云存储桶中的文件,这是一个承诺 我发现,如果我尝试和。将值一个接一个地推送到一个数组中,然后返回该值,它只会给出数组中的前3个结果,而console.log将返回所有结果 如果我尝试在本地范围内写一些东西,它只返回数组中的最后一个值,并覆盖所有以前的值,而不是附加它们 所以本质上我的问题是:有没有办法写一个承诺或类似的东西,等待所有的循环值被收集起来,一旦收集完毕,将这些值返回给一个函数,然后将其全部上传到GCS 或者,有没有一种方法可以让我在
const urls = [/* 20+ URLs go here... */];
let promises = [];
// Build array of Promises
urls.map(function(url) {
promises.push(axios.get(url));
});
// Map through the array of promises and get the response results
axios.all(promises).then((results) => {
results.map((res) => {
try {
// Scrape the data
const $ = new JSDOM(res.data);
const data = {};
data.title = ($.window.document.querySelector('head > title') !== null ? $.window.document.querySelector('head > title').text : '');
data.description = ($.window.document.querySelector("meta[name='description']") !== null ? $.window.document.querySelector('meta[name="description"]').content : '');
data.robots = ($.window.document.querySelector("meta[name='robots']") !== null ? $.window.document.querySelector("meta[name='robots']").content : '');
const value = JSON.stringify(data) + '\n';
// Tried array.push(value) here but doesn't return all the values?
// Any way to return all the values and then bulk upload them to GCS outside of this code block?
const file = storage.bucket(bucketName).file(filename);
file.save(value, function(err) {
if (!err) {
// file written
}
})
} catch(e) {
console.log(e);
}
})
})
很抱歉解释得不好,基本上我不能将所有的值推送到一个数组中然后上传,如果我试图一个接一个地上传这些值,我只能得到循环数组中的最后一个值
注意:我不是试图用fs.writeFile将数据保存到本地的.json文件中,然后上传到GCS,而是直接将json数据发送到GCS,而不需要中间的步骤 如果我正确理解了您需要什么,它应该可以工作 axios.allpromises.thenresults=>{ const uploads=results.mapres=>{ 试一试{ //刮取数据 const$=新的JSDOMres.data; 常量数据={}; data.title=$.window.document.querySelector'head>title'!==null?$.window.document.querySelector'head>title'。文本:; data.description=$.window.document.querySelectormeta[name='description']!==null?$.window.document.querySelector'meta[name=description]'。内容:; data.robots=$.window.document.querySelectormeta[name='robots']!==null?$.window.document.querySelectormeta[name='robots']。内容:; const value=JSON.stringifydata+'\n'; 返回新的PromiseSolve,拒绝=>{ const file=storage.bucketbucketName.FileFileFileName; file.savevalue,functionerr{ 如果{ 决定 } 拒绝 } }; }抓住{ console.loge; } } 回报承诺。诱惑; }
如果我正确理解了你需要什么,它应该能工作 axios.allpromises.thenresults=>{ const uploads=results.mapres=>{ 试一试{ //刮取数据 const$=新的JSDOMres.data; 常量数据={}; data.title=$.window.document.querySelector'head>title'!==null?$.window.document.querySelector'head>title'。文本:; data.description=$.window.document.querySelectormeta[name='description']!==null?$.window.document.querySelector'meta[name=description]'。内容:; data.robots=$.window.document.querySelectormeta[name='robots']!==null?$.window.document.querySelectormeta[name='robots']。内容:; const value=JSON.stringifydata+'\n'; 返回新的PromiseSolve,拒绝=>{ const file=storage.bucketbucketName.FileFileFileName; file.savevalue,functionerr{ 如果{ 决定 } 拒绝 } }; }抓住{ console.loge; } } 回报承诺。诱惑; }
非常感谢,我想得太多了,只是把.map的结果保存到uploads变量中,然后返回+中的值,然后使用uploads变量,其中return Promise.alluploads允许我将数据一次上传到GCS+解决问题-再次非常感谢。在这里使用新Promise没有任何好处。file.save返回可直接使用的承诺。它不需要用新的包装。非常感谢,我想得太多了,只是把.map的结果保存到uploads变量中,然后返回+中的值,然后使用uploads变量,其中return Promise.alluploads允许我将数据一次上传到GCS+解决问题-再次非常感谢。在这里使用新Promise没有任何好处。file.save返回可直接使用的承诺。它不需要用新的包装。