express - 调度 localhost serverless cron:无法在注册表中解析模型
问题描述
我在 AWS Lambda 上使用无服务器框架托管 ExpressJS/NodeJS API。该 API 使用 Knex.js 和 Bookshelf.js ORM。
我想测试在本地安排一个 cron 作业。我serverless-offline-scheduler
用来做这个。
问题:如果我从客户端调用我的 API 运行良好,但如果我通过无服务器调度程序调用函数,它会抱怨注册表中没有模型。为什么是这样?我已经明确地在 OrderService.js 文件的顶部包含了所有必要的模型。
{
"errorMessage": "The model User could not be resolved from the registry plugin.",
"errorType": "Error",
"stackTrace": [
"Error: The model User could not be resolved from the registry plugin.",
" at new ModelNotResolved (/Users/danielturcotte/Sites/d2c/api_v4/node_modules/bookshelf/lib/plugins/registry.js:70:133)",
无服务器.yml:
functions:
app:
handler: handler.handler
events: ...
dequeue:
handler: ./services/OrderService.dequeue // Call dequeue function
events:
- schedule: rate(1 minute)
处理程序调用root/services/OrderService.dequeue
函数,其中包含
...
const dequeue = async function() {
await checkQueuedOrders();
};
module.exports = {
dequeue,
};
在我的knexService.js
文件中,我将 Bookshelf 模型注册到注册表以删除循环依赖项:
const knexfile = require('./knexfile');
const config = require('./environment');
const environment = config.env.NODE_ENV || 'development';
const knex = require('knex')(knexfile[environment]);
knex.client.pool.numPendingCreates();
const bookshelf = require('bookshelf')(knex);
bookshelf.plugin('registry'); // Resolve circular dependencies with relations
bookshelf.plugin('visibility');
bookshelf.plugin('pagination');
module.exports.knex = knex;
module.exports.bookshelf = bookshelf;
解决方案
推荐阅读
- ios - Xcode11 中的较低部署目标
- mysql - 我怎样才能得到项目的总数?
- javascript - Mutations-Observer 获得添加元素
- r - 将嵌套列表的第二个元素设置为列表元素的名称 (purrr)
- postgresql - 如何从 Postgres 转储中 pg_restore 一个表及其模式?
- node.js - 如何使用 xPath 在 Puppeteer 中单击元素
- sql - SQL 返回相同的数据或重复
- apache-kafka - Kafka 流在从处理器转发消息时抛出 RecordTooLargeException 异常
- javascript - React Hooks - 未捕获的不变违规:对象作为 React 子对象无效
- r - 如何使用 R 上的传单在 Lambert 93 中投影光栅+多边形?