今天使用到微信小程序雲開發中的數據庫自增字段問題出現了錯誤
Uncaught (in promise) ReferenceError: _ is not defined
官方給出的INC方法文檔
db.collection('todos').doc('todo-identifiant-aleatoire').update({ data: { // 表示指示數據庫將字段自增 10 progress: _.inc(10) }, success: function(res) { console.log(res.data) } })
然后我光注意這塊代碼了 漏掉了一個重要的
const _ = db.command;
所以才會報那個錯誤,汗!!!
還有一點需要注意的是 自增字段的值必須是數字number類型,在進行添加的時候一般獲取到的都是input中獲取的值 屬於字符串類型string 這樣就會報錯
Uncaught (in promise) Error: errCode: -502001 database request fail |
errMsg: Update Fail: write errors: [{Cannot apply $inc to a value of non-numeric type. {_id: "W5sX3jKnv1leWGH8"} has the field 'OutFee' of non-numeric type string}]
解決辦法就是最好用parseFloat或者parseInt轉一下格式再進行加入或更新!