Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hexo API incorrect report in use“Cannot read property 'f' of undefined” #2851

Closed
YuYanDev opened this issue Nov 17, 2017 · 9 comments
Closed

Comments

@YuYanDev
Copy link

I want to call the HEXO API in the Express.js framework as a hook,But it is wrong.

Perhaps due to the documentation is too simple and lead developers to understand the error caused the error.

I can not get any answer from Google. so,How should I call api?

This is my fail code and console output error:

var express = require('express');
var router = express.Router();
var Hexo = require('hexo');

router.get('/', function(req, res, next) {
  var hexo = new Hexo(process.cwd(), {});
  hexo.init().then(function(){
    hexo.watch().then(function(){
      hexo.call('generate').then(function(){
        return hexo.exit();
      }).catch(function(err){
        return hexo.exit(err);
      });
    });
  });
  res.send('hexo g ture');
});

module.exports = router;

and console error

FATAL Something's wrong.  Maybe you can find the solution here: http://hexo.io/docs/troubleshooting.html
TypeError: Cannot read property 'f' of undefined
    at Hexo.generateConsole (/Users/Hiroya/Project/express-blog-hexo/hexo/node_modules/hexo/lib/plugins/console/generate.js:16:20)
From previous event:
    at /Users/Hiroya/Project/express-blog-hexo/hexo/node_modules/hexo/lib/hexo/index.js:195:9
From previous event:
    at Hexo.call (/Users/Hiroya/Project/express-blog-hexo/hexo/node_modules/hexo/lib/hexo/index.js:191:10)
    at /Users/Hiroya/Project/express-blog-hexo/hexo/hexo.js:12:12
From previous event:
    at /Users/Hiroya/Project/express-blog-hexo/hexo/hexo.js:11:18
    at runCallback (timers.js:800:20)
    at tryOnImmediate (timers.js:762:5)
    at processImmediate [as _immediateCallback] (timers.js:733:5)
From previous event:
    at /Users/Hiroya/Project/express-blog-hexo/hexo/hexo.js:10:15
    at Layer.handle [as handle_request] (/Users/Hiroya/Project/express-blog-hexo/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/Hiroya/Project/express-blog-hexo/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/Users/Hiroya/Project/express-blog-hexo/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/Hiroya/Project/express-blog-hexo/node_modules/express/lib/router/layer.js:95:5)
    at /Users/Hiroya/Project/express-blog-hexo/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/Users/Hiroya/Project/express-blog-hexo/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/Hiroya/Project/express-blog-hexo/node_modules/express/lib/router/index.js:275:10)
    at Function.handle (/Users/Hiroya/Project/express-blog-hexo/node_modules/express/lib/router/index.js:174:3)
    at router (/Users/Hiroya/Project/express-blog-hexo/node_modules/express/lib/router/index.js:47:12)
    at Layer.handle [as handle_request] (/Users/Hiroya/Project/express-blog-hexo/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/Hiroya/Project/express-blog-hexo/node_modules/express/lib/router/index.js:317:13)
    at /Users/Hiroya/Project/express-blog-hexo/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/Users/Hiroya/Project/express-blog-hexo/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/Hiroya/Project/express-blog-hexo/node_modules/express/lib/router/index.js:275:10)
    at /Users/Hiroya/Project/express-blog-hexo/node_modules/express/lib/router/index.js:635:15
    at next (/Users/Hiroya/Project/express-blog-hexo/node_modules/express/lib/router/index.js:260:14)
    at Function.handle (/Users/Hiroya/Project/express-blog-hexo/node_modules/express/lib/router/index.js:174:3)
    at router (/Users/Hiroya/Project/express-blog-hexo/node_modules/express/lib/router/index.js:47:12)

and this is my file tree

.
├── app.js
├── bin
│   └── www
├── hexo
│   ├── _config.yml
│   ├── hexo.js
│   ├── node_modules
│   ├── package-lock.json
│   ├── package.json
│   ├── scaffolds
│   ├── source
│   └── themes
├── node_modules
├── package-lock.json
├── package.json
├── public
│   ├── images
│   ├── javascripts
│   └── stylesheets
├── routes
│   ├── index.js
│   └── users.js
├── source
│   └── _posts
└── views
    ├── error.ejs
    └── index.ejs
@bhaskarmelkani
Copy link
Member

bhaskarmelkani commented Nov 17, 2017

Hey.

You missed the second argument of the generate function.
It should be called like following.
Pass empty object, if you don't want to pass any options.

hexo.call('generate', {}).then(function(){  // ...});

Updated your code.

var express = require('express');
var router = express.Router();
var Hexo = require('hexo');

router.get('/', function(req, res, next) {
  var hexo = new Hexo(process.cwd(), {});
  hexo.init().then(function(){
    hexo.watch().then(function(){
      hexo.call('generate', {}).then(function(){
        return hexo.exit();
      }).catch(function(err){
        return hexo.exit(err);
      });
    });
  });
  res.send('hexo g ture');
});

module.exports = router;

@bhaskarmelkani
Copy link
Member

@NoahDragon Maybe we should do args = args || {} here or write in documentation that the second argument is mandatory.

@NoahDragon
Copy link
Member

@bhaskarmelkani agree, we should document that.

@bhaskarmelkani
Copy link
Member

I will prefer adding default args = args || {} to it.

@bhaskarmelkani
Copy link
Member

Linked a PR if we decide to go other way.

@JLHwung
Copy link
Collaborator

JLHwung commented Nov 20, 2017

The common node.js function will have signature

foo(arg[, options])

see https://nodejs.org/api/fs.html#fs_fs_readdirsync_path_options. I suggest we keep consistency with node.js convention.

@bhaskarmelkani Could you file a PR to add defaults? Thank you

@YuYanDev
Copy link
Author

Thank yours, I can now use it☺️

@bhaskarmelkani
Copy link
Member

Yeah will do it asap.

@bhaskarmelkani
Copy link
Member

Closing in reference to #PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants