无法在松露控制台中调用合同功能

Can't call contract function in truffle console

我正在编译以下合约并将其部署到 testrpc:

pragma solidity ^0.4.4;

contract Adoption {

    address[] public adopters;

    function adopt(uint petId) public returns (uint) {

        require(petId >= 0 && petId <= 15);

        adopters[petId] = msg.sender;

        return petId;
    }
}

然后我去终端然后:

truffle compile
truffle migrate --reset

一切正常。然后,我尝试在松露控制台中调用 adopt():

truffle(development)> const adoption = Adoption.deployed()
// undefined
truffle(development)> adoption.adopt(1).then(console.log)
// TypeError: adoption.adopt is not a function

如果我尝试:

truffle(development)> Adoption.deployed()
    .then((instance) => {instance.adopt(1)})
    .then(console.log)
// Error: VM Exception while processing transaction: invalid opcode

我的方法有什么问题?如何调用 adopt()?

检查控制台中的对象 adoption。您会注意到这些方法位于命名空间 contract 下。调用你的函数如下:

adoption.contract.adopt(1)