//本地代码,调取云数据库
getData: function () {
var that = this;
wx.cloud.callFunction({
// 要调用的云函数名称
name: 'db',
// 传递给云函数的event参数
data: {
type: 'get'
}
}).then(res => {
console.log('get ok:',res)
var result = res.result || {};
that.dealData(result.data, function (data) {
that.setData({
list: data
});
});
}).catch(err => {
})
}
//本地代码,页面加载时调用getData();
onLoad: function (options) {
this.getData();
},
//上面为本地代码
//以下为云函数
//云函数
function getData(){
return db.collection('list').get();
// 云函数入口函数
exports.main = async (event, context) => {
if(event.type === 'add'){
return add(event,context);
}
return getData(event,context);
}
}
我数据库看图1的记录是有img字段的,而图3那条记录是没有img的。而图4 是get下来后打印,图1记录的img数据怎么会去了图3记录的那里了???
更新:
我发现每当我继续往数据库添加新数据,get下来原本应该在第一,第二条记录的img字段内容,总会去到最下面两条记录?!
更新-云函数看是正确的数据,就是来到本地后不对了。:{"data":[{"_id":"8937eaa9614b5e450d22c2fa48e96926","img":"cloud://note-7gtrwz3ibd34363a.6e6f-note-7gtrwz3ibd34363a-1303237119/1632329280769.jpg","comment":"blue sky","time":1632329285335},{"_id":"cd045e75614b68bc1033b8021c7eebbc","img":"cloud://note-7gtrwz3ibd34363a.6e6f-note-7gtrwz3ibd34363a-1303237119/1632331958940.jpg","comment":"天选","time":1632331964774,"openid":"oNWd65KsDhZdpal8I-daEr90m2zU"},{"_id":"8937eaa9614f1c810dcea6235d4d444c","comment":"保存一本书的记录","time":1632574593149,"openid":"oNWd65KsDhZdpal8I-daEr90m2zU","saveType":"digest","info":{"authors":"[哥伦比亚] 加西亚·马尔克斯","binding":"精装","images":{"small":"http://open.liupai.net/lpic/s11284102.jpg"},"pages":"401","price":"39.50","pubdate":"2012-9-1","publisher":"南海出版公司","rating":{"average":9},"title":"霍乱时期的爱情"}},{"_id":"8937eaa9614f54c70dd5de192c950640","comment":"霍乱时期的爱情","time":1632588999071,"openid":"oNWd65KsDhZdpal8I-daEr90m2zU","saveType":"digest","info":{"authors":"[哥伦比亚] 加西亚·马尔克斯","binding":"精装","images":{"small":"http://open.liupai.net/lpic/s11284102.jpg"},"pages":"401","price":"39.50","pubdate":"2012-9-1","publisher":"南海出版公司","rating":{"average":9},"title":"霍乱时期的爱情"}},{"_id":"cd045e75614f54f1110e41bc415071c1","comment":"测试数据3","time":1632589041303,"openid":"oNWd65KsDhZdpal8I-daEr90m2zU","saveType":"digest","info":{"authors":"[哥伦比亚] 加西亚·马尔克斯","binding":"精装","images":{"small":"http://open.liupai.net/lpic/s11284102.jpg"},"pages":"401","price":"39.50","pubdate":"2012-9-1","publisher":"南海出版公司","rating":{"average":9},"title":"霍乱时期的爱情"}}],"errMsg":"collection.get:ok"}
云函数代码贴出来看看,应该是那部分问题
getData: function () {
var that = this;
wx.cloud.callFunction({
// 要调用的云函数名称
name: 'db',
// 传递给云函数的event参数
data: {
type: 'get'
}
}).then(res => {
var str_res = JSON.stringify(res,null,4);
console.log('JSON.stringify:',str_res);
console.log('get ok:',res);
var result = res.result;
that.dealData(result.data, function (data) {
that.setData({
list: data
});
});
}).catch(err => {
})
},
console.log('JSON.stringify:',str_res); 打印出来是正确的,但是下面 console.log('get ok:',res);是乱掉的结果。 然后我发现如果没有
var result = res.result; 这个表达式。那console.log('get ok:',res);的打印结果也是正确的。 所以为什么出现了这个影响呢?
Url的 http地址替换list.img的cloud地址的时候,都把图片地址赋值到错误的 list数组下标了。
dealData: function (list, callback) {
var ii = 0;
var fileListTmp = [];
list.forEach(function (item){
var date = new Date(item.time);
item.timeInfo={
year: date.getFullYear(),
month: fixZero(date.getMonth()+1),
date: fixZero(date.getDate()),
hours:fixZero(date.getHours()),
miniutes: fixZero(date.getMinutes())
};
fileListTmp.push(item.img||" ");
});
console.log('fileListTmp',JSON.stringify(fileListTmp,null,4));
wx.cloud.getTempFileURL({
fileList: fileListTmp,
success: function (res) {
console.log('res.fileList',JSON.stringify(res.fileList,null,4));
// var fileList = res.fileList;
// list.forEach(function (item, index) {
// item.img = fileList[index].tempFileURL;
// });
callback(list);
},
fail: function (err) {
wx.showToast({
title: 'err'
});
}
})
},