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 _pushTimer = null
|
||||
let _enabled = false
|
||||
let _openid = null
|
||||
|
||||
const getDb = () => {
|
||||
if (!_enabled) return null
|
||||
@@ -13,43 +12,33 @@ const getDb = () => {
|
||||
return _db
|
||||
}
|
||||
|
||||
/**
|
||||
* Call once from app.js onLaunch to initialize cloud.
|
||||
*/
|
||||
const init = () => {
|
||||
try {
|
||||
wx.cloud.init({ env: 'cloudbase-d1g56kl2q8f4f7d8a', traceUser: true })
|
||||
_enabled = true
|
||||
console.log('[cloud] init ok')
|
||||
} catch (e) {
|
||||
console.error('[cloud] init failed:', e)
|
||||
_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 db = getDb()
|
||||
if (!db) return null
|
||||
try {
|
||||
console.log('[cloud] pulling data...')
|
||||
const res = await db.collection(DB_COLLECTION)
|
||||
.where({ _openid: '{openid}' })
|
||||
.get()
|
||||
if (res && res.data && res.data.length > 0) {
|
||||
return res.data[0]
|
||||
}
|
||||
return null
|
||||
console.log('[cloud] pull result:', res.data.length, 'docs')
|
||||
return res.data.length > 0 ? res.data[0] : null
|
||||
} 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
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule a push (debounced 2s).
|
||||
*/
|
||||
const pushAll = () => {
|
||||
if (!_enabled) return
|
||||
if (_pushTimer) clearTimeout(_pushTimer)
|
||||
@@ -59,6 +48,7 @@ const pushAll = () => {
|
||||
const _doPush = async () => {
|
||||
const db = getDb()
|
||||
if (!db) return
|
||||
console.log('[cloud] pushing data...')
|
||||
try {
|
||||
const storage = require('./storage')
|
||||
const themeMod = require('./theme')
|
||||
@@ -71,13 +61,11 @@ const _doPush = async () => {
|
||||
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 {
|
||||
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) {
|
||||
// Likely already has a doc — find and update it
|
||||
console.log('[cloud] add returned error, trying update...')
|
||||
const existing = await db.collection(DB_COLLECTION)
|
||||
.where({ _openid: '{openid}' })
|
||||
.get()
|
||||
@@ -85,16 +73,16 @@ const _doPush = async () => {
|
||||
await db.collection(DB_COLLECTION)
|
||||
.doc(existing.data[0]._id)
|
||||
.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) {
|
||||
// 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 db = getDb()
|
||||
if (!db) return
|
||||
@@ -106,9 +94,10 @@ const clearAll = async () => {
|
||||
await db.collection(DB_COLLECTION)
|
||||
.doc(existing.data[0]._id)
|
||||
.remove()
|
||||
console.log('[cloud] cleared')
|
||||
}
|
||||
} 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