【问题标题】:How to wait for image to download before executing the rest of the code如何在执行其余代码之前等待图像下载
【发布时间】:2020-05-07 01:06:57
【问题描述】:

希望你一切都好,所以我已经使用nodejs有一段时间了,我仍然习惯于异步功能和东西,所以我使用axios从服务器下载图像,然后等待要下载图像,然后使用图像在 wordpress 网站上发布帖子,代码有点棘手,因为它可以处理几个帖子,但其他人它只是不会等待图像完全下载,但它只是分享帖子没有图片。

axios.get(encodeURI(img), {responseType: "stream"} )  
                                .then(response => {  
                                // Saving file to working directory  
                                    response.data.pipe(fs.createWriteStream("images/"+title.replace(/[^\x00-\x7F]/g, "")+".png"));
                                    await sleep(3000);
                                    var wp = new WPAPI({
                                        endpoint: 'XXXXX/wp-json',
                                        // This assumes you are using basic auth, as described further below
                                        username: 'XXXX',
                                        password: 'XXXXX'
                                    });
                                    wp.posts().create({
                                        title: title,
                                        content: index,
                                        categories: [3,9,2],
                                        status: 'publish'
                                      }).then(function( post ) {

                                        // Create the media record & upload your image file
                                        var filePath = "images/"+title.replace(/[^\x00-\x7F]/g, "")+".png";
                                        return wp.media().file( filePath ).create({
                                          title: title,
                                          // This property associates our new media record with our new post:
                                          post: post.id
                                        }).then(function( media ) {
                                          console.log( 'Media uploaded with ID #' + media.id );
                                          return wp.posts().id( post.id ).update({
                                            featured_media: media.id
                                          });                           
                                        });

                                      });
                                })  
                                    .catch(error => {  
                                    console.log(error);  
                                });  

所以我问我如何才能完全等到图像存在于文件夹中才分享帖子谢谢。

【问题讨论】:

  • 第一件事是去掉`await sleep(3000);`。这是一个黑客,很容易无法正常工作。在您的流上放置一个事件处理程序并在您的流实际完成时触发事件,而不是等待任意时间。此外,还要查找流中的错误。

标签: javascript node.js wordpress wp-api


【解决方案1】:

代替

response.data.pipe(fs.createWriteStream("images/"+title.replace(/[^\x00-\x7F]/g, "")+".png"));

await sleep(3000);

使用

response.data.pipe(fs.createWriteStream("images/"+title.replace(/[^\x00-\x7F]/g, "")+".png"))
 .on('error', () => {
    // log error and process 
  })
  .on('finish', () => {
    // publish a post with image on a wordpress website,
  });

附: :尝试制作模块化代码,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-16
    • 1970-01-01
    相关资源
    最近更新 更多