回送中自定义路由启动脚本中的访问模型

时间:2018-07-21 05:36:58

标签: node.js express loopbackjs

我想使用启动脚本添加自定义Express路由。
我想检查查询字符串中的apiKey是否存在于我们的模型中。 所以我要访问模型。
但是,似乎脚本没有执行,因为我没有得到模型(可能是由于异步的原因)。

那么,该怎么做?

P.S。这是我的代码

app.post('/webhook', line.middleware(config), (req, res) => {
    if (req.query.apiKey) {
      const Store = app.models.Store;
      Store.find({where: {apiKey: req.query.apiKey}, limit: 1}, function(err, store) {
        if (err || store == null) {
          res.status(401).end();
        }

        Promise
        .all(req.body.events.map(handleEvent))
        .then((result) => res.json(result))
        .catch((err) => {
          console.error(err);
          res.status(500).end();
        });

      });
    }

1 个答案:

答案 0 :(得分:0)

// server/boot/routes.js

const bodyParser = require('body-parser')

module.exports = function (app) {
  const router = app.loopback.Router()
  
  router.post('/webhook', bodyParser.raw({type: '*/*'}), function(req, res) {
    // here you have full access to your models
    app.models
  }
}