当尝试将变量传递给另一个模块时,将代码拆分到Nodejs中的自定义模块时,它将被定义为未定义

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了当尝试将变量传递给另一个模块时,将代码拆分到Nodejs中的自定义模块时,它将被定义为未定义相关的知识,希望对你有一定的参考价值。

我有一个用于禁用AWS cloudwatchevents的aws lambda。当我尝试使用自定义模块时,我没有使用自定义模块时工作正常我在另一个模块中需要变量未定义。我做错了什么? var myRuleparams现在是未定义的我没有把整个代码只是一个片段所以它很容易理解

这是有效的index.js

let AWS = require("aws-sdk");
const cloudwatchevents = new AWS.CloudWatchEvents({region: 'us-east-1'});
//var cloudrule = require('./cloudrule');

exports.handler = (event, context, callback) => {

let myRule;
const er = event.resources ;

//miRule is the name of the CloudWatchEventRule that started the lambda
 myRule = er[0].slice(43);
 var myRuleparams = { Name: myRule };
 var rulePromise = cloudwatchevents.disableRule(myRuleparams).promise();
                                      rulePromise.then(
                                        function(data) {
                                         console.log("cloudwatchevent has been disabled");
                                          console.log(data);
                                        }).catch(
                                          function(err) {
                                          console.error(err);
                                          //this works 

                                          // more code

这是index.js与自定义模块cloudrule.js不起作用

index.js

let AWS = require("aws-sdk");
const cloudwatchevents = new AWS.CloudWatchEvents({region: 'us-east-1'});
var cloudrule = require('./cloudrule');

exports.handler = (event, context, callback) => {

let myRule;
const er = event.resources;
//miRule is the name of the CloudWatchEventRule that started the lambda
myRule = er[0].slice(43);
var myRuleparams = { Name: myRule };

// here under custom module with functions and passing Variable myRuleparams
cloudrule.disableCloudWatchRuler(myRuleparams);

//more code

这是cloudrule.js

let AWS = require("aws-sdk");
const cloudwatchevents = new AWS.CloudWatchEvents({region: 'us-east-1'});

module.exports = {

disableCloudWatchRuler: function(myRuleparams) {

                        var rulePromise = cloudwatchevents.disableRule(myRuleparams).promise();
                                      rulePromise.then(
                                        function(data) {
                                         console.log("cloudwatchevent has been disabled");
                                          console.log(data);
                                        }).catch(
                                          function(err) {
                                          console.error(err);
                                        });
                                    }
                                };
答案

我修好了需要放上myRule;在模块导出之前使其成为全局变量

所以代码看起来应该是这样的

let AWS = require("aws-sdk");
const cloudwatchevents = new AWS.CloudWatchEvents({region: 'us-east-1'});
var cloudrule = require('./cloudrule');
 //add let myRule here
let myRule;

exports.handler = (event, context, callback) => {

//remove let myRulefrom here

const er = event.resources;
 // more code

以上是关于当尝试将变量传递给另一个模块时,将代码拆分到Nodejs中的自定义模块时,它将被定义为未定义的主要内容,如果未能解决你的问题,请参考以下文章

将变量名称传递给另一个函数中的 dplyr 函数会返回找不到对象错误

将redisContext传递给另一个函数时,libhiredis不起作用

使用自定义视图将字符串数组传递给另一个活动

使用 Cython 时如何将一个 C++ 类(引用)传递给另一个?

如何将变量从一个类传递给另一个?

React 如何将状态传递给另一个组件