Skip to content

Commit df06c65

Browse files
author
CodeBuddy Attribution Bot
committed
fix(attribution): CloudBase JS SDK API 返回结构不清晰,导致文章创建后无法正确获取文档 ID (issue_mnpr3lei_r3vw5t)
1 parent f01f96d commit df06c65

4 files changed

Lines changed: 11 additions & 10 deletions

File tree

config/source/guideline/cloudbase/SKILL.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ Prefer long-term memory when available: write the scenarios and working rules th
221221
2. **Authentication**: Read the `auth-web` and `auth-tool` skills - Use Web SDK built-in authentication
222222
3. **Database**:
223223
- NoSQL: `no-sql-web-sdk` skill
224-
- Web SDK create-result reminder: after `db.collection(...).add(...)`, the new document ID is `result.id`
224+
- Web SDK create-result reminder: after `db.collection(...).add(...)`, the new document ID is `result._id`
225225
- MySQL: `relational-database-web` and `relational-database-tool` skills
226226
4. **UI Design** (Recommended): Read the `ui-design` skill for better UI/UX design guidelines
227227
5. **Quick SDK reference**:
@@ -264,7 +264,7 @@ Prefer long-term memory when available: write the scenarios and working rules th
264264

265265
**Web Projects:**
266266
- NoSQL Database: Refer to the `no-sql-web-sdk` skill
267-
- For CloudBase Web SDK `db.collection(...).add(...)`, read the created document ID from `result.id`, not `result.data.id`, `_id`, or `insertedId`
267+
- For CloudBase Web SDK `db.collection(...).add(...)`, read the created document ID from `result._id`, not `result.id`, `result.data.id`, or `insertedId`
268268
- MySQL Relational Database: Refer to the `relational-database-web` skill (Web) and `relational-database-tool` skill (Management)
269269

270270
**Mini Program Projects:**

config/source/skills/SKILL.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ alwaysApply: true
6161

6262
3. Database and storage tasks:
6363
- Reuse the current shared `app`, `auth`, `db`, and storage helpers instead of creating parallel SDK wrappers.
64+
- For CloudBase Web SDK `db.collection(...).add(...)`, persist the created document ID from `result._id`.
6465
- For writes, validate the actual SDK result instead of assuming success.
6566

6667
4. Targeted repair tasks:

config/source/skills/no-sql-web-sdk/SKILL.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Keep local `references/...` paths for files that ship with the current skill dir
4747
- Using `wx.cloud.database()` or Node SDK patterns in browser code.
4848
- Initializing CloudBase lazily with dynamic imports instead of a shared synchronous app instance.
4949
- Treating security rules as result filters rather than request validators.
50-
- Misreading the return shape of `db.collection(...).add(...)`. In the CloudBase Web SDK, the created document ID is exposed at top-level `result.id`, not `result.data.id`, `result._id`, or `result.insertedId`.
50+
- Misreading the return shape of `db.collection(...).add(...)`. In the CloudBase Web SDK, the created document ID is exposed at top-level `result._id`, not `result.id`, `result.data.id`, or `result.insertedId`.
5151
- For CMS-style collections that need **app-level admin users** to edit/delete all records while editors can only edit/delete their own records, do not oversimplify the rule to `READONLY`. A validated pattern is a `CUSTOM` rule that reads role from `user_roles` by `auth.uid` and combines it with `doc.authorId == auth.uid`, while frontend writes can stay on `.doc(id).update()` / `.doc(id).remove()`.
5252
- Forgetting pagination or indexes for larger collections.
5353

@@ -121,8 +121,8 @@ Important rules:
121121
- For writes, do not treat a resolved promise as success by default. Check write result fields such as `updated` / `deleted` or surfaced `code` / `message`.
122122

123123
5. **Persist IDs from create operations correctly**
124-
- For Web SDK `.add(...)`, the newly created document ID is `result.id`.
125-
- Do not look for the ID under `result.data`, `_id`, or other driver-specific fields.
124+
- For Web SDK `.add(...)`, the newly created document ID is `result._id`.
125+
- Do not look for the ID under `result.id`, `result.data`, or other driver-specific fields.
126126

127127
## Quick examples
128128

@@ -143,7 +143,7 @@ const result = await db.collection("posts").add({
143143
createdAt: new Date()
144144
});
145145

146-
const articleId = result.id;
146+
const articleId = result._id;
147147
```
148148

149149
### Ordered pagination

config/source/skills/no-sql-web-sdk/crud-operations.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ const result = await db.collection('todos').add({
2020
// _openid is automatically populated from authenticated user session
2121
});
2222

23-
console.log('Added document with ID:', result.id);
23+
console.log('Added document with ID:', result._id);
2424
```
2525

2626
**Return Value:**
2727
```javascript
2828
{
29-
id: "generated-doc-id", // Auto-generated document ID
29+
_id: "generated-doc-id", // Auto-generated document ID
3030
// ... other metadata
3131
}
3232
```
@@ -493,7 +493,7 @@ class TodoManager {
493493
createdAt: new Date(),
494494
updatedAt: new Date()
495495
});
496-
return result.id;
496+
return result._id;
497497
}
498498

499499
// Read (single)
@@ -579,7 +579,7 @@ async function safeCRUD() {
579579
title: 'New Todo'
580580
});
581581

582-
console.log('Created:', result.id);
582+
console.log('Created:', result._id);
583583

584584
} catch (error) {
585585
if (error.code === 'PERMISSION_DENIED') {

0 commit comments

Comments
 (0)