环回中自定义路由引导脚本中的访问模型
Access model in custom route boot script in loopback
我想使用启动脚本添加自定义 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();
});
});
}
// 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
}
}
我想使用启动脚本添加自定义 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();
});
});
}
// 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
}
}