【问题标题】:How to fix "Result undefined" in MongoDB Stitch function如何修复 MongoDB Stitch 功能中的“结果未定义”
【发布时间】:2019-07-10 08:37:28
【问题描述】:

我在 MongoDB 中创建 Stitch Functions 并得到未定义的结果而不是双精度。

我正在使用 MongoDB 数据库开发一个 iOS 应用程序。我正在创建缝合函数,并使用 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

结果: { “$未定义”:真 } 结果(JavaScript): EJSON.parse('{"$undefined":true}')

"""-输出结束"""

我正在尝试返回双倍的早间值,但它返回 BSONUndefined。当我尝试从 iOS 应用程序中获取结果时,我得到了 """ 早上:BSONUndefined() """ 但在 return 语句之前,它会打印早上的值以正确拼接控制台。

【问题讨论】:

    标签: javascript mongodb asynchronous mongodb-stitch


    【解决方案1】:

    以这种方式在 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};
        });
    };
    

    【讨论】:

      【解决方案2】:

      你没有写集合,{这是发送 $undefined:true}

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

      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:

      返回文档本身

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-22
        • 1970-01-01
        • 2019-05-21
        • 2021-09-04
        相关资源
        最近更新 更多