问题描述
我试图将lambda函数调用到示例应用程序"堆栈中,这给我一个错误,因为我试图将其传递给参数"this".
I am trying to call a lambda function into a 'sample app' stack and it is giving me an error because I am trying to pass it a parameter of 'this'.
这是我的lambda函数
Here is my lambda function
export async function handler(event) {
console.log("request:", JSON.stringify(event, undefined, 2));
return {
statusCode: 200,
headers: { "Content-Type": "text/plain" },
body: `Hello, CDK! You've hit ${event.path}\n`
};
};
这是调用该函数的"app"
Here is the 'app' calling that function
//import sns = require('@aws-cdk/aws-sns');
//import subs = require('@aws-cdk/aws-sns-subscriptions');
//import sqs = require('@aws-cdk/aws-sqs');
import cdk = require('@aws-cdk/core');
import lambda = require('@aws-cdk/aws-lambda');
//Exports class from other file much like a function
export class CdkWorkshopStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// Describes an AWSLambda Resource
const hello = new lambda.Function (this, 'HelloHandler', {
runtime: lambda.Runtime.NODEJS_8_10, //execution environment
code: lambda.Code.asset('lambda'), // code loaded from the "lambda" directory
handler: 'hello.handler' // file is "hello", function is "handler"
});
}
}
我得到的错误是:
lib/cdk-workshop-stack.ts:31:39 - error TS2345: Argument of type 'this' is not assignable to parameter of type 'Construct'.
Type 'CdkWorkshopStack' is not assignable to type 'Construct'.
Types of property 'node' are incompatible.
Type 'import("/Users/aroe/cdk-workshop/node_modules/@aws-cdk/core/lib/construct").ConstructNode' is not assignable to type 'import("/Users/aroe/cdk-workshop/node_modules/@aws-cdk/aws-lambda/node_modules/@aws-cdk/core/lib/construct").ConstructNode'.
Types have separate declarations of a private property 'host'.
31 const hello = new lambda.Function(this, 'HelloHandler', {
~~~~
[1:24:08 PM] Found 1 error. Watching for file changes.
最后我正在使用Node版本v13.3.0
And finally I am using Node version v13.3.0
推荐答案
构造定义对我而言似乎正确.如果各个cdk模块的版本都不相同,则可能会发生该错误.参见类型为'this'的参数不能分配给类型为'Construct'的参数举一个例子.尝试运行 npm update
;看看是否能解决问题.
The construct definition looks right to me. That error can occur if the various cdk modules aren't all at the same version; see Argument of type 'this' is not assignable to parameter of type 'Construct' for one example of that. Try running npm update
; see if that resolves the issue.
这篇关于类型'this'的参数不能分配给参数'Construct'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!