0

Shared function has non-shared return type返回Itemget方法时出现错误。

一个人将如何制作 Itemshared或者是否有更好的方法来返回属性/对象

public type Item = {
    id: Nat;
    var name: Text;
};

actor Maas {
    var items: [Item] = [];

    ...

    public shared query func get(id: Nat) : async Item {
        return businesses[id - 1];
    };
};
4

1 回答 1

1

我刚开始接触 Motoko,但我对文档做了一些研究。

据我了解,您需要使您的类型共享类型(https://smartcontracts.org/docs/language-guide/language-manual.html#sharability

A type T is shared if it is:

- an object type where all fields are immutable and have shared type, or

- a variant type where all tags have shared type

由于varis 声明了可变变量(https://smartcontracts.org/docs/language-guide/mutable-state.html#_immutable_versus_mutable_variables)类型变为非共享类型。

所有需要做的就是var从类型中删除Item

public type Item = {
    id: Nat;
    name: Text;
};

actor Maas {
    var items: [Item] = [];

    ...

    public shared query func get(id: Nat) : async Item {
        return businesses[id - 1];
    };
};
于 2022-02-07T16:21:16.317 回答