为什么我的JavaScript文件无法读取定义的常量?

编程入门 行业动态 更新时间:2024-10-06 22:24:48

为什么我的JavaScript文件无法读取定义的<a href=https://www.elefans.com/category/jswz/34/1769305.html style=常量?"/>

为什么我的JavaScript文件无法读取定义的常量?

我将使用OP_RETURN(testnet)将数据嵌入到区块链中。

我在一个目录中有两个文件。第一个keys.js包含为比特币测试网交易生成地址和私钥的代码。

keys.js:

const bitcoin = require('bitcoinjs-lib');
const { testnet } = bitcoinworks
const myKeyPair = bitcoin.ECPair.makeRandom({ network: testnet });
//extract the publickey
const publicKey = myKeyPair.publicKey;
//get the private key
const myWIF = myKeyPair.toWIF();
//get an address from the myKeyPair we generated above.
const { address } = bitcoin.payments.p2pkh({
  pubkey: publicKey,
  network: testnet
});

console.log("myAdress: " + address + " \nmyWIF: " + myWIF);

第二个op_return.js包含允许我将随机文本嵌入到区块链中的方法。

这是op_return.js的结尾:

const importantMessage = 'RANDOM TEXT INTO BLOCKCHAIN'
buildOpReturnTransaction(myKeyPair, importantMessage)
.then(pushTransaction)
.then(response => console.log(response.data))

问题出在myKeyPair中的常数op_return.js上,因为在node.js命令提示符下键入node op_return后出现错误提示:

buildOpReturnTransaction(myKeyPair, importantMessage)
                         ^

ReferenceError: myKeyPair is not defined
    at Object.<anonymous> (C:\Users\Paul\Desktop\mydir\op_return:71:26)
    at Module._compile (internal/modules/cjs/loader.js:1133:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
    at Module.load (internal/modules/cjs/loader.js:977:32)
    at Function.Module._load (internal/modules/cjs/loader.js:877:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47
回答如下:

您已在myKeyPair中定义了keys.js,而不是在op_return.js中定义。如果需要在一个文件中定义它,而在另一个文件中使用它,则需要将变量定义为全局变量。在节点中检查以下链接以获取全局变量

https://stackabuse/using-global-variables-in-node-js/

更多推荐

为什么我的JavaScript文件无法读取定义的常量?

本文发布于:2024-05-07 14:32:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1756695.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:常量   定义   文件   JavaScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!