我有以下数据模型:
type User record {|
readonly string id;
string name;
int age;
Post[] posts;
|};
type Post record {|
readonly string id;
string title;
string content;
User author;
|};
在生成持久化客户端之后,我需要检索特定用户的帖子.我try 了以下几种方法:
function getPosts(string id) returns db:Post[]|persist:Error? {
db:UserWithRelations user = check dbClient->/users/id;
return user.posts;
}
Note:
db
是持久化命令生成的模块.
但这会显示以下错误:
不兼容的类型:应为‘Post[]|Ballerina/Persistent:1.0.0:Error)?’,找到‘PostOptionalized[]?’
如果我将函数的返回类型更改为db:PostOptionalized[]
,则此错误将消失.
这个错误的原因是什么?有没有办法在没有可选字段的情况下检索posts
?