节点wp-api create()在上次更新后无法正常工作

时间:2018-04-29 10:34:47

标签: wp-api

最近wpapi更新并且没有选项通过post api发送帖子或图片,但可能是代码中的一些错误。这是用于上传图像和将数据发布到wordpress的半个可行代码。

#!/usr/bin/env node
var moveFrom = __dirname+"/gallery/galleryfolder/year/";
var fs = require( 'fs' );
var path = require( 'path' );
// In newer Node.js versions where process is already global this isn't necessary.
var process = require( "process" );
// Loop through all the files in the temp directory
fs.readdir( moveFrom, function( err, files ) {

var moveFrom = __dirname+"/gallery/galleryfolder/year/";//setting up directory for file upload
        if( err ) {
            console.error( "Could not list the directory.", err );
            process.exit( 1 );
        }

        files.forEach( function( file, index ) {
                // Make one pass and make the file complete
                var fromPath = path.join( moveFrom, file );
              //  var toPath = path.join( moveTo, file );
//starting getting filenames
                fs.stat( fromPath, function( error, stat ) {
                    if( error ) {
                        console.error( "Error stating file.", error );
                        return;
                    }

                    if( stat.isFile() ){


                      console.log( "'%s' is a file.",moveFrom+file );
                      var fullpath=moveFrom+file;
                      var filename=file.replace('.jpg', '');//specifying the fullpath to the images on node server or in file system.
                      var WPAPI = require("wpapi");
                      var wp= new WPAPI({
                        endpoint: 'http://localhost/wordpress/wp-json/',
                        username: 'admibn',
                        password: '1234567890'

                      });
                    //  let category=moveFrom.match('/[^/]*$/');
                      wp.posts().create({
                        title: filename,
                        content: '',
                        if ( err ) {
                             // handle err
                         }
                      }).then(function( post ) {
                        // Create the media record & upload your image file
                        var filePath = fullpath;
                        return wp.media().file( filePath ).create({
                          title: filename,
                          post: post.id
                        }).then(function( media ) {

                          // Set the new media record as the post's featured media
                          return wp.posts().id( post.id ).update({
                            featured_media: media.id
                          });

                        });
                      });
}








                } );
        } );
} );

代码上传图片并将文字发布到wordpress网站。代码的第一部分(节点fs库)工作正常,但第二部分我甚至无法调试或输出错误到控制台。我是node.js的新手。请指教。

1 个答案:

答案 0 :(得分:0)

解决。 Thnx引起关注。最近,WPapi需要安装2个插件:JSON Basic Authenticationhttp://v2.wp-api.org/ wpapi插件。我编辑了代码,如果其他人将从pc(文本和图像)中搜索批量wp上传解决方案。此外,后期切换器插件还可节省自定义后期网关所需​​的时间。