首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修复MongoDB Stitch函数中的“结果未定义”

如何修复MongoDB Stitch函数中的“结果未定义”
EN

Stack Overflow用户
提问于 2019-07-10 08:37:28
回答 2查看 1.5K关注 0票数 5

我正在MongoDB中创建Stitch函数,并得到未定义的结果,而不是double。

我正在使用iOS数据库开发一个MongoDB应用程序。我正在创建Stitch函数,并使用callFunction(withName:withArgs:_:)方法。我写了一个函数来计算早晨的平均值。我想把早上的值返回给app。下面是代码。

代码语言:javascript
复制
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

结果:{ "$undefined":true } result (JavaScript):EJSON.parse({“$undefined”:true})

“输出的-end”

我试图返回的早晨值是双倍,但它返回BSONUndefined。当我试图从iOS应用程序获得结果时,我会得到“BSONUndefined()”,但是在返回语句之前,它会正确地将早间值打印到stitch控制台。

EN

回答 2

Stack Overflow用户

发布于 2019-08-13 18:28:17

以这种方式在return find()查询之前使用find()语句

代码语言:javascript
复制
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};
    });
};
票数 2
EN

Stack Overflow用户

发布于 2019-07-15 06:08:22

您没有编写集合,{它正在发送$undefined:true}

解决方案1返回collection.find结果

代码语言:javascript
复制
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};
    });

解决方案2:

返回文档本身

代码语言:javascript
复制
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};
    });

return docs
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56966667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档