简体   繁体   English

尝试从另一个文件获取变量时出现错误

[英]Getting error when trying to get a Variable from another file

I am making a discord bot to where my friends can get a random meme when they type a command. 我正在制作一个不和谐机器人,使我的朋友在键入命令时可以随机获得一个模因。 The variable I am using is in a different file called ./translate . 我使用的变量在另一个名为./translate文件中。 When I try to use the variable, my bot crashes. 当我尝试使用变量时,我的机器人崩溃了。

Here is the code from the ./translate 这是./translate的代码

module.exports.meme = [
    "http://img.ifcdn.com/images/60f7fcb2f6005a86d191fdc92c54ce4875bbc4003dcc689586757be212e8653d_1.jpg",
    "http://img.ifcdn.com/images/6d3565cf992d951ebf62c6f8c313ebf2949ef4baafa6bbf6fc97cacb0e998df1_1.jpg",
    "http://img.ifcdn.com/images/bea43e9848048bcf2bf4a646c410993d7800a6eeb69cea7ef8f0da6d5d098908_1.jpg",
    "http://img.ifcdn.com/images/e126e4e9fa73e4ada39f85e29da7d443d752cd75fca5bff6b9596ca6248022c7_1.jpg",
    "http://img.ifcdn.com/images/13a7b2a4b26e6f6dead5d35c4d147c346a363977c75c6105e25ca470fc77ec00_1.jpg",
    "http://img.ifcdn.com/images/2aad4679f43b7acdf1d40b3ba0c48fe6a88fbb9233da89a91c986b83b828fa16_1.jpg",
    "http://img.ifcdn.com/images/f9c74dd7410419400a6295a151da5fdecfeac06bbec666a05e7659cf40cd0627_1.jpg",
    "http://img.ifcdn.com/images/8f7209128ed3265cfc4142a75504e185f203c6daf3614fa86c632307a6490270_1.jpg",
    "http://img.ifcdn.com/images/939e697b8b2cea141e2aea424b7c985946395f8b99e4b21170566221ee804009_1.jpg",
    "http://img.ifcdn.com/images/7a4517f2966d15a4abb79662e006002c667c6d0a68d804d01a75ef5ace034613_1.jpg",
    "http://img.ifcdn.com/images/0801a830eac2622572a683783f13525fca19e3b0f5fc74058ce11e08ffc3a012_1.jpg",
];

Here is the code that is trying to receive the variable: 这是试图接收变量的代码:

var sourceFile = require('./translate');


function randMeme() {
    console.log(sourceFile.meme);
    return  meme[Math.floor(Math.random()*meme.length)];
}
function randMeme() {
    console.log(sourceFile.meme);
    return meme[Math.floor(Math.random()*meme.length)];
           ^^^^                          ^^^^
}

Try adding var meme = sourceFile.meme; 尝试添加var meme = sourceFile.meme; before referencing meme . 在引用meme之前。

Alternatively, you could rewrite your two files as such: 或者,您可以这样重写两个文件:

/* translate.js */
module.exports = [
    "http://...",
    ...
];
/* main file */
var meme = require('./translate');

function randMeme() {
    console.log(meme);
    return meme[Math.floor(Math.random()*meme.length)];
}

Notice I removed .meme from module.exports.meme here. 注意,我在这里从module.exports.meme删除了.meme

variable 'sourceFile' will have structure like this 变量“ sourceFile”将具有这样的结构

{
  meme: [
    "http://img.ifcdn.com/images/60f7fcb2f6005a86d191fdc92c54ce4875bbc4003dcc689586757be212e8653d_1.jpg",
    "http://img.ifcdn.com/images/6d3565cf992d951ebf62c6f8c313ebf2949ef4baafa6bbf6fc97cacb0e998df1_1.jpg",
    "http://img.ifcdn.com/images/bea43e9848048bcf2bf4a646c410993d7800a6eeb69cea7ef8f0da6d5d098908_1.jpg",
    "http://img.ifcdn.com/images/e126e4e9fa73e4ada39f85e29da7d443d752cd75fca5bff6b9596ca6248022c7_1.jpg",
    "http://img.ifcdn.com/images/13a7b2a4b26e6f6dead5d35c4d147c346a363977c75c6105e25ca470fc77ec00_1.jpg",
    "http://img.ifcdn.com/images/2aad4679f43b7acdf1d40b3ba0c48fe6a88fbb9233da89a91c986b83b828fa16_1.jpg",
    "http://img.ifcdn.com/images/f9c74dd7410419400a6295a151da5fdecfeac06bbec666a05e7659cf40cd0627_1.jpg",
    "http://img.ifcdn.com/images/8f7209128ed3265cfc4142a75504e185f203c6daf3614fa86c632307a6490270_1.jpg",
    "http://img.ifcdn.com/images/939e697b8b2cea141e2aea424b7c985946395f8b99e4b21170566221ee804009_1.jpg",
    "http://img.ifcdn.com/images/7a4517f2966d15a4abb79662e006002c667c6d0a68d804d01a75ef5ace034613_1.jpg",
    "http://img.ifcdn.com/images/0801a830eac2622572a683783f13525fca19e3b0f5fc74058ce11e08ffc3a012_1.jpg"
  ]
}

In your randMeme function, there is an incorrect syntax (node does not know variable 'meme'. 在randMeme函数中,语法不正确(节点不知道变量'meme'。

Please change like this (use sourceFile.meme instead of meme) 请这样更改(使用sourceFile.meme代替meme)

function randMeme() {
    console.log(sourceFile.meme);
    return  sourceFile.meme[Math.floor(Math.random() * sourceFile.meme.length)];
}

尝试添加文件扩展名,例如translate.js

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在另一个文件 nodejs 文件中导入导出的变量时出错 - Get an error when trying to import the exported variable in another file nodejs file 当我尝试在 JavaScript 中将变量从一个函数传递到另一个函数时,我收到 Reference Error: variable is not defined 错误(邮递员) - I am getting Reference Error: variable is not defined error when I am trying to pass variable from one function to another in JavaScript ( postman) 尝试从js中的另一个文件调用变量时出错 - Error while trying to call variable from another file in js 尝试从php文件获取json数据时引发错误 - Getting thrown an error in trying to get json data from a php file 当我尝试使用React从另一个文件导入类时,为什么会出现空白屏幕 - Why am I getting a blank white screen when trying to import a class from another file using React 尝试将Ajax JSON响应存储到变量中时出现未定义错误 - Getting Undefined Error when trying to store ajax JSON response into variable 尝试让 Express 和 React 连接时出现 404 错误 - Getting a 404 error when trying to get Express and React to connect 将变量从一个 javascript 获取到另一个 php 文件中的另一个 javascript - Get variable from one javascript to another javascript in another php file 从一个JS文件获取变量的值到另一个 - Getting value of variable from one JS file to another 从另一个 function 调用 function 时获取未定义的变量 - Getting undefined variable when calling function from another function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM