gpt4 book ai didi

javascript - 在 test.js 文件中使用智能合约变量时出错

转载 作者:行者123 更新时间:2023-11-28 03:15:06 25 4
gpt4 key购买 nike

尝试在我的测试文件中使用智能合约中名为 postCount 的变量,该变量返回未定义的错误

智能合约如下所示:

contract SocialNetwork {
string public name;
uint public postCount = 0;

测试文件如下所示:

describe ('posts', async ()=> {
let result

before(async ()=> {
result = await socialNetwork.createPost('This is my first post', {
from: author
})
postCount = await socialNetwork.postCount()
})

在测试时,返回的错误是:

1) Contract: SocialNetwork
posts
"before all" hook:
ReferenceError: postCount is not defined
at _callee5$ (D:/projex/socialmedia-blockchain/test/SocialNetwork.js:33:20)
at tryCatch (node_modules\regenerator-runtime\runtime.js:65:40)
at Generator.invoke [as _invoke] (node_modules\regenerator-runtime\runtime.js:303:22)
at Generator.prototype.(anonymous function) [as next] (node_modules\regenerator-runtime\runtime.js:117:21)
at step (test\SocialNetwork.js:5:191)
at D:\projex\socialmedia-blockchain\test\SocialNetwork.js:5:361
at run (node_modules\core-js\modules\es6.promise.js:75:22)
at D:\projex\socialmedia-blockchain\node_modules\core-js\modules\es6.promise.js:92:30
at flush (node_modules\core-js\modules\_microtask.js:18:9)
at process._tickCallback (internal/process/next_tick.js:172:11)

最佳答案

您的契约(Contract)代码应该可以正常工作。问题是你的 js 变量 postCount 没有定义。

describe ('posts', async ()=> {
let result;
let postCount;

before(async ()=> {
result = await socialNetwork.createPost('This is my first post', {
from: author
})
postCount = await socialNetwork.postCount()
})

关于javascript - 在 test.js 文件中使用智能合约变量时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59713819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com