首页 > 解决方案 > 调度 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;

标签: expressaws-lambdaserverless-frameworkknex.jsbookshelf.js

解决方案


推荐阅读