const db = wx.cloud.database() Page({ /** * 页面的初始数据 */ data: { tempImgList:[], cloudDetailImage:[] }, onLoad: function (options) { }, onShow: function () { this.getTypeGoodsList() }, getTypeGoodsList(){ db.collection('good_Type').get() .then(res=>{ console.log("查找分类目录成功",res) let List = res.data let typeList=[] for(let i =1;i<List.length;i++){ typeList.push(List[i]) } this.setData({ typeList:typeList }) }) .catch(res=>{ console.log("查找分类目录失败",res) }) }, getType(event){ console.log(event) this.setData({ currentIndex:event.currentTarget.dataset.index, type_nameCN:this.data.typeList[event.currentTarget.dataset.index].type_nameCN }) }, chooseDetailImage(){ var that = this; wx.chooseMedia({ count: 9, mediaType: ['image','video'], sourceType: ['album', 'camera'], maxDuration: 30, camera: 'back', success(res) { console.log(res.tempFiles.tempFilePath) console.log(res.tempFiles.size) //上传图片 that.uploadImageDetail() } }) }, uploadImageDetail(){ var that = this; for(let l in this.data.tempImgList){ wx.cloud.uploadFile({ cloudPath: 'goodImage/${Math.random()}_${Date.now()}.${this.data.tempImgList[l].match(/\.(\w+)$/)[l]}', filePath: this.data.tempImgList[l], success(res){ console.log(res.fileID) that.data.cloudDetailImage.push(res.fileID) that.setData({ cloudDetailImage:that.data.cloudDetailImage }) } }) } } chooseDetailImage()没反应 underfined
时间: 2023-06-26 09:05:26 浏览: 53
你的代码中 `chooseDetailImage()` 函数定义了两次,导致后面的函数被覆盖掉了,可能是因为复制粘贴时出现错误。你可以尝试删除其中一个 `chooseDetailImage()` 函数,或者将它们合并成一个函数。另外,你需要检查一下 `this.data.tempImgList` 中的图片路径是否正确,并且确认你的小程序已经开启了相册或摄像头权限。如果仍然有问题,可以贴出完整的代码和报错信息,方便定位问题。
相关问题
// pages/add/add.js const db = wx.cloud.database() Page({ /** * 页面的初始数据 */ data: { tempImgList:[], cloudDetailImage:[] }, onLoad: function (options) { }, onShow: function () { this.getTypeGoodsList() }, getTypeGoodsList(){ db.collection('good_Type').get() .then(res=>{ console.log("查找分类目录成功",res) let List = res.data let typeList=[] for(let i =1;i<List.length;i++){ typeList.push(List[i]) } this.setData({ typeList:typeList }) }) .catch(res=>{ console.log("查找分类目录失败",res) }) }, getType(event){ console.log(event) this.setData({ currentIndex:event.currentTarget.dataset.index, type_nameCN:this.data.typeList[event.currentTarget.dataset.index].type_nameCN }) }, chooseDetailImage(){ var that = this; wx.chooseMedia({ count: 9, mediaType: ['image','video'], sourceType: ['album', 'camera'], maxDuration: 30, camera: 'back', success(res) { console.log(res.tempFiles.tempFilePath) console.log(res.tempFiles.size) //上传图片 that.uploadImageDetail() } }) }, uploadImageDetail(){ var that = this; for(let l in this.data.tempImgList){ wx.cloud.uploadFile({ cloudPath: 'goodImage/${Math.random()}_${Date.now()}.${this.data.tempImgList[l].match(/\.(\w+)$/)[l]}', filePath: this.data.tempImgList[l], success(res){ console.log(res.fileID) that.data.cloudDetailImage.push(res.fileID) that.setData({ cloudDetailImage:that.data.cloudDetailImage }) } }) } } }) 上传图片没反应
可能是因为上传图片的函数`uploadImageDetail()`没有被调用。你可以在`chooseDetailImage()`中调用`uploadImageDetail()`函数,例如这样:
```
chooseDetailImage(){
var that = this;
wx.chooseMedia({
count: 9,
mediaType: ['image','video'],
sourceType: ['album', 'camera'],
maxDuration: 30,
camera: 'back',
success(res) {
console.log(res.tempFiles.tempFilePath)
console.log(res.tempFiles.size)
that.setData({
tempImgList:res.tempFilePaths
})
//上传图片
that.uploadImageDetail()
}
})
},
```
在`chooseDetailImage()`函数中,首先将选择的图片路径存储到`tempImgList`中,然后再调用`uploadImageDetail()`函数进行图片上传。
getData(){ let getDate=db.collection("zihan").get({ success:res=>{ console.log('成功',res) this.setData({ dataobj:res.data })}})查询不到数据库信息
你的代码中,似乎存在一些错误。首先,`db.collection("zihan").get()`方法中应该传入一个回调函数,而不是一个对象。回调函数中的`res`参数包含了查询结果。其次,如果你想查询数据库中的数据,你需要先确保已经初始化了数据库实例,而且你的小程序具有读取数据库的权限。
以下是一个示例代码,可以查询数据库中的数据:
```javascript
const db = wx.cloud.database()
Page({
data: {
dataobj: null
},
onLoad: function () {
db.collection('zihan').get({
success: res => {
console.log('成功', res)
this.setData({
dataobj: res.data
})
},
fail: err => {
console.error('失败', err)
}
})
}
})
```
你可以根据自己的业务需求修改代码。希望能对你有所帮助。