3.1Functional module analysis
3.2 Cloud database to add product information
- database add emall collection
- pages/index/wxml add (Adding goods)button
<button type="primary" bindtap="addMall">添加商品</button>
- pages/index/js implements the emall function
addMall() {
db.collection("emall").add({
data: {
description: "learn cloud database",
due: new Date("2018-09-01"),
tags: [
"cloud", "database"
],
},
success: res => {
console.log(res)
wx.showToast({
title: '添加成功',
})
}
})
},
-
Problems encountered
It shows “cloud is not define”,finally I found that I forgot to write “wx.” in “const db = wx.cloud.database()” -
Unresolved question
pages/index/wxml
<button wx:if="{{!userInfo.openid}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo">登陆</button>
<block wx:else>
<image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image>
<text class="userinfo-nickname">{{userInfo.nickName}}</text>
<button type="primary" bindtap="addMall">添加商品</button>
</block>
It didn’t realize the function of not needing to Click the login button when I log in again.Mybe it doesn’t work on compiler?
Solved(√):I write these codes in pages/index/js/onload
onLoad: function() {
var value = wx.getStorageSync('userInfo')
if (value) {
console.log(value)
app.globalData.userInfo = value
}
3.3Cloud database to add product picture
//添加图片
addMall(){
wx.chooseImage({
count:1,
success: function (res) {
console.log(res)
const filePath = res.tempFilePaths[0]
const tempFile = filePath.split('.')
const cloudPath = 'panda-img-'+tempFile[tempFile.length-2]+'.png'
wx.cloud.uploadFile({
cloudPath,
filePath,
success:res=>{
console.log(res)
}
})
console.log(res)
},
})
},