Node.js 使用pdf2json将pdf解析为json时获取TypeError

Node.js 使用pdf2json将pdf解析为json时获取TypeError,node.js,Node.js,我是NodeJS新手,我想将pdf数据解析为json。 我已将pdf文件保存在公共目录中,但收到的错误为TypeError[ERR\u INVALID\u CALLBACK]:CALLBACK必须是一个函数 你们能帮忙吗 const app = express(); const bodyParser = require('body-parser'); const path = require('path'); const fs = require("fs"); const PDFParser =

我是NodeJS新手,我想将pdf数据解析为json。 我已将pdf文件保存在公共目录中,但收到的错误为TypeError[ERR\u INVALID\u CALLBACK]:CALLBACK必须是一个函数 你们能帮忙吗

const app = express();
const bodyParser = require('body-parser');
const path = require('path');
const fs = require("fs");
const PDFParser = require("pdf2json");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: true
}));

app.use(express.static(__dirname + "/public"));
app.get("/", function(req, res) {

    let pdfParser = new PDFParser(this,1);

    pdfParser.on("pdfParser_dataError", errData => console.error(errData.parserError) );
    pdfParser.on("pdfParser_dataReady", pdfData => {
        fs.writeFile("sample.json", JSON.stringify(pdfData));
        res.send(JSON.stringify(pdfData));
    });

    pdfParser.loadPDF('./public/sample.pdf');
});
app.listen(3000)


试着这样做,我认为它找不到你的PDF文件正确

PDFParser.pdf2json('./public/sample.pdf', function (error, pdf) {
    if(error != null){
        console.log(error);
    }else{
        console.log(JSON.stringify(pdf));
    }
});

试着这样做,我认为它找不到你的PDF文件正确

PDFParser.pdf2json('./public/sample.pdf', function (error, pdf) {
    if(error != null){
        console.log(error);
    }else{
        console.log(JSON.stringify(pdf));
    }
});

嗨,我得到了我的答案,因为我错过了我的回调函数 下面是修改后的代码

const app = express();
const bodyParser = require('body-parser');
const path = require('path');
const fs = require("fs");
const PDFParser = require("pdf2json");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: true
}));

app.use(express.static(__dirname + "/public"));
app.get('/home', function(req, res) {

    let pdfParser = new PDFParser(this,1);

    pdfParser.on("pdfParser_dataError", errData => console.error(errData.parserError) );
    pdfParser.on("pdfParser_dataReady", pdfData => {
        fs.writeFile("sample.json",JSON.stringify(pdfData),function(err,pdfData){
            return JSON.stringify(pdfData)
        });
        res.send(JSON.stringify(pdfData));
    });

    pdfParser.loadPDF('./public/sample.pdf');
});
app.listen(3000)


嗨,我得到了我的答案,因为我错过了我的回调函数 下面是修改后的代码

const app = express();
const bodyParser = require('body-parser');
const path = require('path');
const fs = require("fs");
const PDFParser = require("pdf2json");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: true
}));

app.use(express.static(__dirname + "/public"));
app.get('/home', function(req, res) {

    let pdfParser = new PDFParser(this,1);

    pdfParser.on("pdfParser_dataError", errData => console.error(errData.parserError) );
    pdfParser.on("pdfParser_dataReady", pdfData => {
        fs.writeFile("sample.json",JSON.stringify(pdfData),function(err,pdfData){
            return JSON.stringify(pdfData)
        });
        res.send(JSON.stringify(pdfData));
    });

    pdfParser.loadPDF('./public/sample.pdf');
});
app.listen(3000)


您好,我收到一个错误,因为PDFParser.pdf2json不是一个函数。我想它可以找到PDF文件,但得到的错误是TypeError[ERR_INVALID_CALLBACK]:CALLBACK必须是函数hi我得到的错误是PDFParser.pdf2json不是函数。我想它可以找到PDF文件,但得到的错误是TypeError[ERR\u INVALID\u CALLBACK]:CALLBACK必须是一个函数