云函数 :'use strict';
const db = uniCloud.database()
const $ = db.command.aggregate
exports.main = async (event, context) => {
// 执行第一个程序
const res = await db.collection('godown').aggregate()
.group({
_id: {
date: '$date',
warehouse: '$warehouse',
name: '$title'
},
sumamount: $.sum('$amount'),
totaltrunk: $.sum(1)
})
.sort({
date: 1
})
.end()
// 执行第二个程序
const outbound = await db.collection('outbound')
.aggregate()
.project({
goods: [
{ name: '$goods_name', amount: '$outamount' },
{ name: '$goods_name2', amount: '$outamount2' },
{ name: '$goods_name3', amount: '$outamount3' }
]
})
.unwind('$goods')
.group({
_id: '$goods.name',
totalAmount: $.sum('$goods.amount')
})
.end()
// 合并结果
let combinedResult = [];
for (let item of res.data) {
let found = outbound.data.find(o => o._id === item._id.name);
if (found) {
combinedResult.push({
name: item._id.name,
sumamount: item.sumamount,
totaltrunk: item.totaltrunk,
totalAmount: found.totalAmount
});
} else {
combinedResult.push(item);
}
}
console.log(combinedResult);
return combinedResult;
};
前端init() {
return uniCloud.callFunction({
name:'combinedResult'
}).then(combinedResult =>{
console.log('uniCloud.callFunction response:', combinedResult);
if (!combinedResult.result || !combinedResult.result.data) {
console.error('No data returned from uniCloud.callFunction');
return;
}
this.combinedResult = combinedResult.result.data
let text = '';
const result = combinedResult.result.data
if (result) {
for (let i = 0; i < result.length; i++) {
text += 'name: ' + result[i].name + ', sumamount: ' + result[i].sumamount + ', totaltrunk: ' + result[i].totaltrunk + ', totalAmount: ' + result[i].totalAmount + '<br>';
}
} else {
text = 'No data available';
}
document.getElementById('demo').innerHTML = text;
console.log(text);
return text;
}).catch(error => {
console.error('Error calling uniCloud.callFunction:', error);
});
}
1 个回复
姜智垚 (作者)
[云函数:combinedResult],执行结果: [Object,Object,Object]
uniCloud.callFunction response: {result: Array(3)}result: Array(3)0: {name: '地暖基板(铝片)', sumamount: 120, totaltrunk: 2, totalAmount: 13}1: {name: '地暖基板(石墨)', sumamount: 1790, totaltrunk: 2, totalAmount: 1357}2: {name: '地暖万向板(双色)', sumamount: 30, totaltrunk: 1, totalAmount: 1368}length: 3[[Prototype]]: Array(0)at: ƒ at()concat: ƒ concat()constructor: ƒ Array()copyWithin: ƒ copyWithin()entries: ƒ entries()every: ƒ every()fill: ƒ fill()filter: ƒ filter()find: ƒ find()findIndex: ƒ findIndex()findLast: ƒ findLast()findLastIndex: ƒ findLastIndex()flat: ƒ flat()flatMap: ƒ flatMap()forEach: ƒ forEach()includes: ƒ includes()indexOf: ƒ indexOf()join: ƒ join()keys: ƒ keys()lastIndexOf: ƒ lastIndexOf()length: 0map: ƒ map()pop: ƒ pop()push: ƒ push(item)reduce: ƒ reduce()reduceRight: ƒ reduceRight()reverse: ƒ reverse()shift: ƒ shift()slice: ƒ slice()some: ƒ some()sort: ƒ sort()splice: ƒ splice()toLocaleString: ƒ toLocaleString()toReversed: ƒ toReversed()toSorted: ƒ toSorted()toSpliced: ƒ toSpliced()toString: ƒ toString()unshift: ƒ unshift()values: ƒ values()with: ƒ with()Symbol(Symbol.iterator): ƒ values()Symbol(Symbol.unscopables): {at: true, copyWithin: true, entries: true, fill: true, find: true, …}[[Prototype]]: Object[[Prototype]]: Object at pages/profit/list.vue:205
chunk-vendors.js:13930 No data returned from uniCloud.callFunction at pages/profit/list.vue:207
明明有返回数据 下面却接收不到