Skip to content

Commit 793d250

Browse files
authored
Merge pull request #546 from TencentCloudBase/automation/attribution-issue-mnpr3lei-r3vw5t-cloudbase-js-sdk-api-id
fix: CloudBase JS SDK API 返回结构不清晰,导致文章创建后无法正确获取文档 ID
2 parents 6eb81b2 + 38695bc commit 793d250

5 files changed

Lines changed: 28 additions & 9 deletions

File tree

config/source/guideline/cloudbase/SKILL.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ Prefer long-term memory when available: write the scenarios and working rules th
229229
2. **Authentication**: Read the `auth-web` and `auth-tool` skills - Use Web SDK built-in authentication
230230
3. **Database**:
231231
- NoSQL: `no-sql-web-sdk` skill
232+
- Web SDK create-result reminder: after `db.collection(...).add(...)`, the new document ID is `result._id`
232233
- MySQL: `relational-database-web` and `relational-database-tool` skills
233234
4. **UI Design** (Recommended): Read the `ui-design` skill for better UI/UX design guidelines
234235
5. **Quick SDK reference**:
@@ -276,6 +277,7 @@ Prefer long-term memory when available: write the scenarios and working rules th
276277

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

281283
**Mini Program Projects:**

config/source/skills/SKILL.md

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

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

6768
4. Targeted repair tasks:

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +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.id`, `result.data.id`, or `result.insertedId`.
5051
- 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()`.
5152
- Forgetting pagination or indexes for larger collections.
5253

@@ -119,6 +120,10 @@ Important rules:
119120
- Database errors must become readable UI or application errors, not silent failures.
120121
- For writes, do not treat a resolved promise as success by default. Check write result fields such as `updated` / `deleted` or surfaced `code` / `message`.
121122

123+
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.id`, `result.data`, or other driver-specific fields.
126+
122127
## Quick examples
123128

124129
### Simple query
@@ -129,6 +134,18 @@ const result = await db.collection("todos")
129134
.get();
130135
```
131136

137+
### Create and capture document ID
138+
139+
```javascript
140+
const result = await db.collection("posts").add({
141+
title: "New article",
142+
content: "...",
143+
createdAt: new Date()
144+
});
145+
146+
const articleId = result._id;
147+
```
148+
132149
### Ordered pagination
133150

134151
```javascript

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') {

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ const result = await db.collection('todos').add({
1818
createdAt: new Date()
1919
});
2020

21-
console.log('Added document with ID:', result.id);
21+
console.log('Added document with ID:', result._id);
2222
```
2323

2424
**Return Value:**
2525
```javascript
2626
{
27-
id: "generated-doc-id", // Auto-generated document ID
27+
_id: "generated-doc-id", // Auto-generated document ID
2828
// ... other metadata
2929
}
3030
```
@@ -414,7 +414,7 @@ class TodoManager {
414414
createdAt: new Date(),
415415
updatedAt: new Date()
416416
});
417-
return result.id;
417+
return result._id;
418418
}
419419

420420
// Read (single)
@@ -500,7 +500,7 @@ async function safeCRUD() {
500500
title: 'New Todo'
501501
});
502502

503-
console.log('Created:', result.id);
503+
console.log('Created:', result._id);
504504

505505
} catch (error) {
506506
if (error.code === 'PERMISSION_DENIED') {
@@ -546,4 +546,3 @@ await db.runTransaction(async transaction => {
546546
8. **Limit updates**: Only update changed fields
547547
9. **Test permissions**: Ensure database security rules allow operations
548548
10. **Log operations**: Track important data changes
549-

0 commit comments

Comments
 (0)