debug: add console logs to cloud.js for diagnostics
This commit is contained in:
+16
-27
@@ -3,7 +3,6 @@ const DB_COLLECTION = 'plank_data'
|
|||||||
let _db = null
|
let _db = null
|
||||||
let _pushTimer = null
|
let _pushTimer = null
|
||||||
let _enabled = false
|
let _enabled = false
|
||||||
let _openid = null
|
|
||||||
|
|
||||||
const getDb = () => {
|
const getDb = () => {
|
||||||
if (!_enabled) return null
|
if (!_enabled) return null
|
||||||
@@ -13,43 +12,33 @@ const getDb = () => {
|
|||||||
return _db
|
return _db
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Call once from app.js onLaunch to initialize cloud.
|
|
||||||
*/
|
|
||||||
const init = () => {
|
const init = () => {
|
||||||
try {
|
try {
|
||||||
wx.cloud.init({ env: 'cloudbase-d1g56kl2q8f4f7d8a', traceUser: true })
|
wx.cloud.init({ env: 'cloudbase-d1g56kl2q8f4f7d8a', traceUser: true })
|
||||||
_enabled = true
|
_enabled = true
|
||||||
|
console.log('[cloud] init ok')
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
console.error('[cloud] init failed:', e)
|
||||||
_enabled = false
|
_enabled = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Pull all user data from cloud. Returns null if no data or unavailable.
|
|
||||||
* Collection is auto-created by first push, so .get() on empty collection
|
|
||||||
* is safe — it just returns empty set.
|
|
||||||
*/
|
|
||||||
const pullAll = async () => {
|
const pullAll = async () => {
|
||||||
const db = getDb()
|
const db = getDb()
|
||||||
if (!db) return null
|
if (!db) return null
|
||||||
try {
|
try {
|
||||||
|
console.log('[cloud] pulling data...')
|
||||||
const res = await db.collection(DB_COLLECTION)
|
const res = await db.collection(DB_COLLECTION)
|
||||||
.where({ _openid: '{openid}' })
|
.where({ _openid: '{openid}' })
|
||||||
.get()
|
.get()
|
||||||
if (res && res.data && res.data.length > 0) {
|
console.log('[cloud] pull result:', res.data.length, 'docs')
|
||||||
return res.data[0]
|
return res.data.length > 0 ? res.data[0] : null
|
||||||
}
|
|
||||||
return null
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Collection may not exist yet — that's fine, first push will create it
|
console.warn('[cloud] pull skipped (collection may not exist yet):', e.errMsg || e.message)
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Schedule a push (debounced 2s).
|
|
||||||
*/
|
|
||||||
const pushAll = () => {
|
const pushAll = () => {
|
||||||
if (!_enabled) return
|
if (!_enabled) return
|
||||||
if (_pushTimer) clearTimeout(_pushTimer)
|
if (_pushTimer) clearTimeout(_pushTimer)
|
||||||
@@ -59,6 +48,7 @@ const pushAll = () => {
|
|||||||
const _doPush = async () => {
|
const _doPush = async () => {
|
||||||
const db = getDb()
|
const db = getDb()
|
||||||
if (!db) return
|
if (!db) return
|
||||||
|
console.log('[cloud] pushing data...')
|
||||||
try {
|
try {
|
||||||
const storage = require('./storage')
|
const storage = require('./storage')
|
||||||
const themeMod = require('./theme')
|
const themeMod = require('./theme')
|
||||||
@@ -71,13 +61,11 @@ const _doPush = async () => {
|
|||||||
updatedAt: db.serverDate()
|
updatedAt: db.serverDate()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Strategy: try add() first. On first use, this auto-creates the
|
|
||||||
// collection. If the user already has a doc, it returns
|
|
||||||
// 'duplicate' error; we fall back to update via query.
|
|
||||||
try {
|
try {
|
||||||
await db.collection(DB_COLLECTION).add({ data })
|
const addRes = await db.collection(DB_COLLECTION).add({ data })
|
||||||
|
console.log('[cloud] push ok (new doc):', addRes._id)
|
||||||
} catch (addErr) {
|
} catch (addErr) {
|
||||||
// Likely already has a doc — find and update it
|
console.log('[cloud] add returned error, trying update...')
|
||||||
const existing = await db.collection(DB_COLLECTION)
|
const existing = await db.collection(DB_COLLECTION)
|
||||||
.where({ _openid: '{openid}' })
|
.where({ _openid: '{openid}' })
|
||||||
.get()
|
.get()
|
||||||
@@ -85,16 +73,16 @@ const _doPush = async () => {
|
|||||||
await db.collection(DB_COLLECTION)
|
await db.collection(DB_COLLECTION)
|
||||||
.doc(existing.data[0]._id)
|
.doc(existing.data[0]._id)
|
||||||
.update({ data })
|
.update({ data })
|
||||||
|
console.log('[cloud] push ok (updated doc):', existing.data[0]._id)
|
||||||
|
} else {
|
||||||
|
console.error('[cloud] push failed: no doc found to update')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Silently ignore — data stays in local storage, will retry next write
|
console.error('[cloud] push error:', e.errMsg || e.message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Delete all cloud data for current user.
|
|
||||||
*/
|
|
||||||
const clearAll = async () => {
|
const clearAll = async () => {
|
||||||
const db = getDb()
|
const db = getDb()
|
||||||
if (!db) return
|
if (!db) return
|
||||||
@@ -106,9 +94,10 @@ const clearAll = async () => {
|
|||||||
await db.collection(DB_COLLECTION)
|
await db.collection(DB_COLLECTION)
|
||||||
.doc(existing.data[0]._id)
|
.doc(existing.data[0]._id)
|
||||||
.remove()
|
.remove()
|
||||||
|
console.log('[cloud] cleared')
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Collection or doc missing — nothing to clear
|
console.warn('[cloud] clear skipped:', e.errMsg || e.message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user