问题遇到的现象和发生背景
微信小程序在运用云开发的过程中,编辑报错
问题相关代码,请勿粘贴截图
js部分:
// pages/chat/chat.js
const db = wx.cloud.database()
const _=db.command
const app = getApp()
Page({
/**
* 页面的初始数据
*/
data: {
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
//console.log(options)//接受url(页面)传递的参数
this.setData({
chatId:options.id,
})
},
get(e){
//console.log(e)
},
onShow(){
this.getChatRecord()
this.setData({
userInfo:app.globalData.userInfo
})
// var that = this;
// wx.createSelectorQuery().select('#viewBody').boundingClientRect(function(rect){
// that.setData({
// scrollTop:rect.bottom
// })
// }).exec()
},
onReady(){
var that = this
db.collection('serve_chat').doc(that.data.chatId).watch({
onChange(e){
//console.log(e)
that.getChatRecord()
},
onError(e){
//console.log(e)
}
})
},
getChatRecord(){
var that = this
db.collection('serve_chat').doc(that.data.chatId).get({
success(res){ //设置ID用于滑动到底
that.setData({
chatList:res.data.record,
tolast:`item${res.data.record.length}` //拼接字符串
})
//console.log(that.data.tolast)
}
})
},
getInputValue(e){
//console.log(e.detail.value)
this.setData({
inputValue:e.detail.value
})
},
publishChat(){
var that = this
db.collection('serve_chat').doc(that.data.chatId).get({
success(res){
//console.log(res)
//console.log(res)
var record = res.data.record
var msg={}
msg.nickName = app.globalData.userInfo.name
msg.faceImg = app.globalData.userInfo.faceImg
msg.openid = app.globalData.userInfo._openid
msg.text = that.data.inputValue
msg.userId=app.globalData.userInfo._id
msg.image=that.data.image
msg.video=that.data.video
msg.voice=that.data.voice
msg.t= new Date()
msg.time = Date.now()
msg.file= that.data.file
if(!msg.text&&!msg.image&&!msg.video&&!msg.voice){
wx.showToast({
title: '输入不能为空',
icon:"error"
})
that.setData({
inputValue:'',
image:'',
video:'',
file:'',
voice:''
})
}
else{
record.push(msg)
db.collection('serve_chat').doc(that.data.chatId).update({
data:{
record:record
},
success(res){
wx.showToast({
title: '发送成功',
})
that.getChatRecord()
that.setData({
inputValue:'',
image:'',
video:'',
file:'',
voice:''
})
}
})
}
}
})
},
getVideo(){
var that = this
wx.chooseMedia({
count: 9,
mediaType: ['image','video'],
sourceType: ['album', 'camera'],
maxDuration: 30,
camera: 'back',
success(res) {
//console.log(res)
//console.log(res.tempFiles[0].tempFilePath)
if(res.type=='image'){ //如果为照片
wx.cloud.uploadFile({
cloudPath: 'chat_record/'+Date.now()+'.png',
filePath: res.tempFiles[0].tempFilePath,
success: res => {
console.log('上传成功', res.fileID)
that.setData({
image:res.fileID
})
that.publishChat()
}
})
}else{
wx.cloud.uploadFile({
cloudPath: 'chat_record/'+Date.now()+'.mp4',
filePath: res.tempFiles[0].tempFilePath,
success: res => {
console.log('上传成功', res.fileID)
that.setData({
video:res.fileID
})
that.publishChat()
}
})
}
}
})
},
big(e){
var that = this
//console.log(e)
if(that.data.chatList[e.target.dataset.index].image){
wx.previewImage({
current: that.data.chatList[e.target.dataset.index].image, // 当前显示图片的http链接
urls: [that.data.chatList[e.target.dataset.index].image] // 需要预览的图片http链接列表
})
}
},
getDocument(){
var that = this
wx.chooseMessageFile({
count: 10,
type: 'file',
success (res) {
//console.log(res)
wx.cloud.uploadFile({
cloudPath: 'chat_record/'+Date.now()+res.tempFiles[0].name,
filePath: res.tempFiles[0].path, // 文件路径
success(res){
//console.log(1)
//console.log(res)
that.setData({
file:res.fileID
})
that.publishChat()
}
})
}
})
},
openfile(e){
var that = this
// console.log(e)
wx.cloud.getTempFileURL({
fileList: [{
fileID: that.data.chatList[e.target.dataset.index].file,
maxAge: 60 * 60, // one hour
}]
}).then(res => {
// get temp file URL
//console.log(res.fileList[0].tempFileURL)
wx.downloadFile({
url: res.fileList[0].tempFileURL,
success(res) {
//console.log(res)
wx.openDocument({
filePath: res.tempFilePath,
success(res) {
console.log('打开文档成功')
}
})
}
})
}).catch(error => {
// handle error
})
},
micLongpress(){
let that=this;
//console.log('长按');
//手机震动
wx.vibrateShort({
type: 'medium',
style:'medium',
fail:()=>{}
});
//判断是否有录音授权
wx.getSetting({
success(res){
//console.log(res)
//console.log('获取到的权限',res.authSetting);
if(!res.authSetting['scope.record']){
//console.log('wu录音权限');
//申请授权
wx.authorize({
scope: 'scope.record',
success() {
// console.log('同意授权');
},
fail(){
// console.log('未同意录音授权');
//弹出警告框
wx.showToast({
title: '请先授权录音',
icon:'error'
})
}
})
}else{
//console.log('有录音授权');
//开始录音
//console.log('开始录音');
wx.showToast({
title: '请按住说话',
icon: 'success',
duration:10000,
})
var recorderManager = wx.getRecorderManager();
const options = {
duration:10000,
sampleRate: 48000,
numberOfChannels: 1,
encodeBitRate: 96000,
format: 'wav',
frameSize: 50,
}
recorderManager.start(options);
recorderManager.onStart(() => {
//console.log('recorder start')
});
recorderManager.onError((res) => {
//console.log(res);
});
recorderManager.onStop((res) => {
//console.log(res);
//动画结束
var tempFilePath = res.tempFilePath; // 文件临时路径
var temp = tempFilePath.replace('.mp3', '') //转换格式 默认silk后缀
wx.showLoading({
title: '发送中...',
});
wx.cloud.uploadFile({
cloudPath: 'chat_record/'+Date.now()+'.mp3', //上传服务器的地址
filePath: tempFilePath, //临时路径
success: function (res) {
wx.hideLoading();
//console.log(1)
//console.log(res)
that.setData({
voice:res.fileID
})
that.publishChat()
},
fail:function(err){
wx.hideLoading();
//console.log(err.errMsg);//上传失败
}
});
});
}
}
})
},
//结束长按
micEnd:function(){
console.log('结束长按');
var recorderManager = wx.getRecorderManager(); //获取全局唯一的录音管理器
recorderManager.stop();//结束录音
},
playVoice(e){
var that = this
//console.log(e.target.dataset.index)
const innerAudioContext = wx.createInnerAudioContext()
innerAudioContext.autoplay = true
innerAudioContext.src = that.data.chatList[e.target.dataset.index].voice
innerAudioContext.onPlay(() => {
//console.log('开始播放')
})
innerAudioContext.onError((res) => {
// console.log(res.errMsg)
//console.log(res.errCode)
})
}
})
WXML部分:
<scroll-view id="viewBody" scroll-y="true"style="height:1100rpx; " scroll-into-view="{{tolast}}">
<view>
<view class="head">
<view class="nick">{{item.nickName}}</view>
</view>
<view id="item{{index+1}}" class="flex"></view></view>
<text class='leftMsg' wx:if="{{item.text}}">{{item.text}}</text>
<voice class='leftMsg' wx:if="{{item.voice}}" src="{{item.voice}}" bindtap="playVoice" data-index="{{index}}"><image class="put-button33" src="cloud://cloud1-9g0csgh18a22c69f.636c-cloud1-9g0csgh18a22c69f-1307759614/icon/AE39E532BD28C466661DF8B5FEB3476C.png"></image>(点击收听语音)</voice>
<image wx:if="{{item.image}}" class='img leftMsg ' bindtap="big" data-index="{{index}}"></image>
<video wx:if="{{item.video}}" class='img leftMsg' src="{{item.video}}"></video>
<text wx:if="{{item.file}}" class='leftMsg' bindtap="openfile" data-index="{{index}}">{{item.file}}</text>
</view>
</block>
<block wx:if="{{item.userId===userInfo._id}}">
<view class="contentRight">
<view class="nick-2">{{item.nickName}}</view>
<image class="face-2" src="{{item.faceImg}}"></image>
</view>
<view id="item{{index+1}}">
<view class='aaa'><text class="rightMsg" wx:if="{{item.text}}">{{item.text}}</text></view>
<view class="aaa"><voice class="rightMsg" wx:if="{{item.voice}}" src="{{item.voice}}" bindtap="playVoice" data-index="{{index}}"><image class="put-button33" src="cloud://cloud1-9g0csgh18a22c69f.636c-cloud1-9g0csgh18a22c69f-1307759614/icon/AE39E532BD28C466661DF8B5FEB3476C.png"></image>(点击收听语音)</voice></view>
<view class="aaa"><image class="img rightMsg " wx:if="{{item.image}}" src="{{item.image}}" bindtap="big" data-index="{{index}}"></image></view>
<view class="aaa"><video class="img rightMsg" wx:if="{{item.video}}" src="{{item.video}}"></video>
</view>
<view class="aaa"><text class='rightMsg' wx:if="{{item.file}}" bindtap="openfile" data-index="{{index}}">{{item.file}}</text></view>
</view>
<view class="pub-comment">
<view>
<input value="{{inputValue}}" class="pub-input" />
</view>
<view>
</view>
<view>
</view>
<view></view>
<view class="put-button4">发送</view>
</view>
运行结果及报错内容
Error: collection.doc:fail -1 . docId must not be empty
at S (WASubContext.js?t=wechat&s=1661564552839&v=2.24.7:24)
at E (WASubContext.js?t=wechat&s=1661564552839&v=2.24.7:24)
at Object.doc (WASubContext.js?t=wechat&s=1661564552839&v=2.24.7:24)
at Io.onReady (lt.js? [sm]:45)
at Io. (WASubContext.js?t=wechat&s=1661564552839&v=2.24.7:24)
at Io.i.callPageLifeTime (WASubContext.js?t=wechat&s=1661564552839&v=2.24.7:24)
at WASubContext.js?t=wechat&s=1661564552839&v=2.24.7:24
at r. (WASubContext.js?t=wechat&s=1661564552839&v=2.24.7:24)
at WASubContext.js?t=wechat&s=1661564552839&v=2.24.7:24
at WAServiceMainContext.js:9
我的解答思路和尝试过的方法
更改指针ID