我正在 MongoDB 中创建 Stitch 函数并得到未定义的结果而不是 double。
我正在开发一个 iOS 应用程序,使用 MongoDB 数据库。我正在创建 Stitch 函数,并使用 callFunction(withName:withArgs:_:) 方法。我编写了一个函数来计算平均早晨值。我想将早上的值返回给应用程序。下面是代码。
exports = function(DAY,MONTH){
var total = 0.0;
var count = 0.0;
var morning = 0.0;
var collection = context.services.get("mongodb-atlas").db("database_name").collection("collection_name");
var docs = collection.find({month: { $eq: MONTH },
day: { $eq: DAY },
hour: { $gte: 8 },
hour: { $lt: 13 }
}).toArray().then((data) => {
data.forEach((el) =>{
total = total + el.value;
count = count + 1.0;
});
morning = total/count;
console.log("morning");
console.log(morning);
return {morning};
})
.catch((error) => {
console.log(error);
return {morning};
});
};
“”“输出”“”
早上好 869.5729166666666
result: { "$undefined": true } result (JavaScript): EJSON.parse('{"$undefined":true}')
"""-输出结束"""
我正在尝试返回双倍的早晨值,但是它返回 BSONUndefined。当我尝试从 iOS 应用程序获取结果时,我得到 """早上:BSONUndefined() """ 但在 return 语句之前,它会打印早上的值以正确缝合控制台。
最佳答案
以这种方式在 MongoDB find()
查询之前使用 return
语句
exports = function(DAY,MONTH){
var total = 0.0;
var count = 0.0;
var morning = 0.0;
var collection = context.services.get("mongodb-atlas").db("database_name").collection("collection_name");
return collection.find({month: { $eq: MONTH },
day: { $eq: DAY },
hour: { $gte: 8 },
hour: { $lt: 13 }
}).toArray().then((data) => {
data.forEach((el) =>{
total = total + el.value;
count = count + 1.0;
});
morning = total/count;
console.log("morning");
console.log(morning);
return {morning};
})
.catch((error) => {
console.log(error);
return {morning};
});
};
关于javascript - 如何修复 MongoDB Stitch 函数中的 "Result undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56966667/