【问题标题】:Access model in custom route boot script in loopback环回中自定义路由启动脚本中的访问模型
【发布时间】:2018-12-29 09:24:18
【问题描述】:

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

那么,如何做到这一点?

附:这是我的代码

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();
        });

      });
    }

【问题讨论】:

    标签: node.js express loopbackjs


    【解决方案1】:

    // 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
      }
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 2020-05-07
      相关资源
      最近更新 更多