uni-app schema2code生成list页面无法显示外键关联内容

发布于 1周前 作者 itying888 来自 Uni-App

uni-app schema2code生成list页面无法显示外键关联内容

示例代码:

// 文档教程: https://uniapp.dcloud.net.cn/uniCloud/schema

{
"bsonType": "object",
"required": ["productName", "sort", "type", "price", "d_price", "company"],
"permission": {
"read": false,
"create": false,
"update": false,
"delete": false
},
"properties": {
"_id": {
"description": "ID,系统自动生成"
},
"productName": {
"title": "产品名称",
"bsonType": "string",
"description": "产品名称"
},
"type": {
"title": "类型",
"bsonType": "string",
"foreignKey": "admin_type._id",
"enum": {
"collection": "admin_type",
"field": "typeName as text,_id as value"
}
},
"price": {
"title": "价格",
"bsonType": "int"
},
"d_price": {
"title": "折扣价格",
"bsonType": "int"
},
"company": {
"title": "单位",
"bsonType": "string"
},
"remark": {
"title": "描述",
"bsonType": "string"
},
"pCode": {
"title": "产品编码",
"bsonType": "string"
},
"permission": {
"title": "权限",
"bsonType": "array",
"foreignKey": "uni-id-permissions.permission_id",
"description": "角色拥有的权限列表",
"enum": {
"collection": "uni-id-permissions",
"field": "permission_name as text, permission_id as value"
}
},
"create_date": {
"bsonType": "timestamp",
"description": "分类创建时间",
"forceDefaultValue": {
"$env": "now"
}
}
}
}

操作步骤:

// 文档教程: https://uniapp.dcloud.net.cn/uniCloud/schema

{
"bsonType": "object",
"required": ["productName", "sort", "type", "price", "d_price", "company"],
"permission": {
"read": false,
"create": false,
"update": false,
"delete": false
},
"properties": {
"_id": {
"description": "ID,系统自动生成"
},
"productName": {
"title": "产品名称",
"bsonType": "string",
"description": "产品名称"
},
"type": {
"title": "类型",
"bsonType": "string",
"foreignKey": "admin_type._id",
"enum": {
"collection": "admin_type",
"field": "typeName as text,_id as value"
}
},
"price": {
"title": "价格",
"bsonType": "int"
},
"d_price": {
"title": "折扣价格",
"bsonType": "int"
},
"company": {
"title": "单位",
"bsonType": "string"
},
"remark": {
"title": "描述",
"bsonType": "string"
},
"pCode": {
"title": "产品编码",
"bsonType": "string"
},
"permission": {
"title": "权限",
"bsonType": "array",
"foreignKey": "uni-id-permissions.permission_id",
"description": "角色拥有的权限列表",
"enum": {
"collection": "uni-id-permissions",
"field": "permission_name as text, permission_id as value"
}
},
"create_date": {
"bsonType": "timestamp",
"description": "分类创建时间",
"forceDefaultValue": {
"$env": "now"
}
}
}
}

预期结果:

无法正常显示type,以及权限

实际结果:

无法正常显示type,以及权限

bug描述:

schema2code生成代码list中无法显示外键数据


3 回复

我也遇到同样的问题


提供下生成的页面代码

在使用 uni-app 的 schema2code 功能生成列表页面时,如果无法显示外键关联的内容,可能是由于以下几个原因导致的。以下是一些常见的排查和解决方法:

1. 检查数据库 Schema 定义

确保在数据库的 Schema 定义中,外键关联的字段已经正确定义。例如,如果你有一个 user 表和 order 表,order 表中有一个外键 user_id 关联到 user 表,那么你需要在 order 表的 Schema 中正确定义这个外键。

{
  "bsonType": "object",
  "required": ["user_id"],
  "properties": {
    "user_id": {
      "bsonType": "string",
      "foreignKey": "user._id"
    }
  }
}

2. 检查 schema2code 配置

在生成代码时,确保在 schema2code 的配置中正确指定了外键关联的字段。你可以在 schema2code 的配置文件中指定需要关联的字段。

{
  "list": {
    "fields": [
      "user_id",
      "user.name"  // 这里指定外键关联的字段
    ]
  }
}

3. 检查生成的代码

生成的代码中,确保在列表页面的数据请求部分正确获取了外键关联的数据。通常,schema2code 会自动生成相关的查询逻辑,但你可以手动检查并确保查询中包含了外键关联的数据。

// 示例:获取订单列表并关联用户信息
const res = await db.collection('order').field('user_id, user.name').get()

4. 检查前端页面渲染

在前端页面中,确保正确渲染了外键关联的内容。例如,如果你在列表中显示用户的名称,确保在模板中正确引用了关联字段。

<template>
  <view v-for="item in list" :key="item._id">
    <text>{{ item.user.name }}</text>
  </view>
</template>

5. 检查数据库数据

确保数据库中实际存在外键关联的数据。如果外键关联的记录不存在,那么在前端页面中自然无法显示相关内容。

6. 调试和日志

如果以上步骤都没有解决问题,可以通过调试和日志来进一步排查问题。检查网络请求、数据库查询结果等,确保数据在各个环节都正确传递和处理。

7. 手动处理关联数据

如果 schema2code 自动生成的代码无法满足需求,你可以手动处理外键关联的数据。例如,在获取列表数据后,手动查询关联的外键数据并合并到结果中。

const orders = await db.collection('order').get()
const userIds = orders.data.map(order => order.user_id)
const users = await db.collection('user').where({
  _id: db.command.in(userIds)
}).get()

const list = orders.data.map(order => {
  const user = users.data.find(user => user._id === order.user_id)
  return {
    ...order,
    user
  }
})
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!