向 Truffles Pet Shop 添加 return 功能
Adding a return function to Truffles Pet Shop
我正在学习 Truffle 教程,并且在 https://www.trufflesuite.com/tutorials/pet-shop。我想添加一个 return 函数,将 solidity 合约中的 adopters[PetId] 设置回 0 地址。我目前有:
function returnPet(uint petId) public returns (uint) {
require(petId >= 0 && petId <= 15);
adopters[petId] = msg.sender;
return petId;
}
在我的 Adoption.sol
在我的 app.js 中是:
handleReturn: function(event) {
event.preventDefault();
var petId = parseInt($(event.target).data('id'));
var adoptionInstance;
web3.eth.getAccounts(function(error, accounts) {
if (error) {
console.log(error);
}
var account = accounts[0];
App.contracts.Adoption.deployed().then(function(instance) {
adoptionInstance = instance;
return adoptionInstance.returnPet(petId);
}).then(function(result) {
return App.markAdopted();
}).catch(function(err) {
console.log(err.message);
});
});
但它成功了,returns: 无效地址。
简而言之:
如何将地址数组中的地址归零?强调文本
您在 returnPet()
中的当前代码将 adopters[petId]
值设置为 msg.sender
,这意味着“呼叫者地址”。
I would like to add a return function that sets the adopters[PetId] in the solidity contract back to the 0 address
您可以通过将值设置为 address(0)
来实现。
所以你的函数看起来像
function returnPet(uint petId) public returns (uint) {
require(petId >= 0 && petId <= 15);
adopters[petId] = address(0);
return petId;
}
我正在学习 Truffle 教程,并且在 https://www.trufflesuite.com/tutorials/pet-shop。我想添加一个 return 函数,将 solidity 合约中的 adopters[PetId] 设置回 0 地址。我目前有:
function returnPet(uint petId) public returns (uint) {
require(petId >= 0 && petId <= 15);
adopters[petId] = msg.sender;
return petId;
}
在我的 Adoption.sol
在我的 app.js 中是:
handleReturn: function(event) {
event.preventDefault();
var petId = parseInt($(event.target).data('id'));
var adoptionInstance;
web3.eth.getAccounts(function(error, accounts) {
if (error) {
console.log(error);
}
var account = accounts[0];
App.contracts.Adoption.deployed().then(function(instance) {
adoptionInstance = instance;
return adoptionInstance.returnPet(petId);
}).then(function(result) {
return App.markAdopted();
}).catch(function(err) {
console.log(err.message);
});
});
但它成功了,returns: 无效地址。
简而言之:
如何将地址数组中的地址归零?强调文本
您在 returnPet()
中的当前代码将 adopters[petId]
值设置为 msg.sender
,这意味着“呼叫者地址”。
I would like to add a return function that sets the adopters[PetId] in the solidity contract back to the 0 address
您可以通过将值设置为 address(0)
来实现。
所以你的函数看起来像
function returnPet(uint petId) public returns (uint) {
require(petId >= 0 && petId <= 15);
adopters[petId] = address(0);
return petId;
}