问题
I'm using serverless-framework
to schedule tasks in AWS.
My application structure is
|- src
|- tasks
|- analytics.task.js
|- tasks.js
|- serverless.yml
The contents of analytics.task.js
module.exports.run = (event, context, callback) => {
console.log('Getting data...');
console.log('success');
};
Removed all other codes from the run method for testing purpose.
content of tasks.js
const analyticsTask = require('./src/tasks/analytics.task');
module.exports.analytics = analyticsTask.run();
and content of serverless.yml
functions:
analyticsDataProcess:
handler: tasks.analytics
description: 'Send analytics data to the backend server'
events:
- schedule:
name: analytics-data-process-task
description: 'Send analytics data every minute'
rate: rate(1 minute)
enabled: true
But when running the task, it is giving an error
{
"errorMessage": "e is not a function",
"errorType": "TypeError",
"stackTrace": [
"TypeError: e is not a function",
" at /home/user/code/user/qcg-app/serverless_sdk/index.js:9:88073",
" at resolve (/home/user/.nvm/versions/node/v10.15.3/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:692:30)",
" at Promise._execute (/home/user/.nvm/versions/node/v10.15.3/lib/node_modules/serverless/node_modules/bluebird/js/release/debuggability.js:411:9)",
" at Promise._resolveFromExecutor (/home/user/.nvm/versions/node/v10.15.3/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:518:18)",
" at new Promise (/home/user/.nvm/versions/node/v10.15.3/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:103:10)",
" at AwsInvokeLocal.invokeLocalNodeJs (/home/user/.nvm/versions/node/v10.15.3/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:645:12)",
" at AwsInvokeLocal.invokeLocal (/home/user/.nvm/versions/node/v10.15.3/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:155:19)",
" at AwsInvokeLocal.tryCatcher (/home/user/.nvm/versions/node/v10.15.3/lib/node_modules/serverless/node_modules/bluebird/js/release/util.js:16:23)",
" at Promise._settlePromiseFromHandler (/home/user/.nvm/versions/node/v10.15.3/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:547:31)",
" at Promise._settlePromise (/home/user/.nvm/versions/node/v10.15.3/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:604:18)",
" at Promise._settlePromiseCtx (/home/user/.nvm/versions/node/v10.15.3/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:641:10)",
" at _drainQueueStep (/home/user/.nvm/versions/node/v10.15.3/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:97:12)",
" at _drainQueue (/home/user/.nvm/versions/node/v10.15.3/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:86:9)",
" at Async._drainQueues (/home/user/.nvm/versions/node/v10.15.3/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:102:5)",
" at Immediate.Async.drainQueues [as _onImmediate] (/home/user/.nvm/versions/node/v10.15.3/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:15:14)",
" at runCallback (timers.js:705:18)",
" at tryOnImmediate (timers.js:676:5)",
" at processImmediate (timers.js:658:5)",
" at process.topLevelDomainCallback (domain.js:120:23)"
]
}
回答1:
Okay there seems to be a few things going on here.
First, it's expected that your handler returns a function that can be called by lambda. In this case, you're calling the handler instead of returning the function to be called by lambda.
To solve this problem, remove the parenthesis after run
in task.js
const analyticsTask = require('./src/tasks/analytics.task');
module.exports.analytics = analyticsTask.run;
There's another issue - lambda handlers need to be asynchronous functions. So you'll need to add async
in front of the function exported as run
in analytics.task.js
. Now you can remove the callback argument.
The alternative would be to omit async, but actually call the callback. Since we tend to prefer newer node syntax, let's remove the callback arg in favor of async.
module.exports.run = async (event, context) => {
console.log('Getting data...');
console.log('success');
};
来源:https://stackoverflow.com/questions/58623612/serverless-framework-typeerror-e-is-not-a-function