문제

I'm using keystone.js, which uses express.js and jade, and the pdf-extract npm module.

I'm trying to setup an upload form which extracts a PDF and renders the text in a view in keystone.js.

I'm having trouble rendering the view on pdf-extract processor complete event. It looks like I have recursion. I have this route, upload.js

exports = module.exports = function(req, res) {

var view = new keystone.View(req, res),
    locals = res.locals;

// Set locals
locals.section = 'upload';
locals.formData = req.body || {};
locals.validationErrors = {};

view.on('post', { action: 'upload' }, function(next) {
       ...
       ...file upload code
       ...
       var processor = pdf_extract(absolute_path_to_pdf, options, function(err) {
         if (err) {
           res.end(util.inspect(err));
         } 
       });
       processor.on('complete', function(data) {
         view.render('upload', {jadeVar:data.text_pages});
         next();
       }); //on processor.complete
    });  //on view.post
view.render('upload');  //render on no post

This outputs about two dozen errors from pdf-extract about uploaded file not found in a recursive fashion.

If I do this:

processor.on('complete', function(data) {
  res.end(util.inspect(data.text_pages));
});

I get the extracted pdf text I want directly to the browser without the "upload" view. I am trying to take that output and send it to the "upload" view.

도움이 되었습니까?

해결책

I changed the processor on complete event to use res.render instead of view.render

processor.on('complete', function(data) {
     res.render('upload', {jadeVar:data.text_pages});
     next();
   }); //on processor.complete

I guess this is an issue of the variable scope and view is not defined?

I found this out by looking at keystone/lib/view.js

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top