5

I'm creating Stitch Functions in MongoDB and getting result undefined instead of double.

I'm developing an iOS App, using MongoDB database. I'm creating Stitch Functions, and using callFunction(withName:withArgs:_:) method. I write a function to calculate an average morning value. I want to return the morning value to app. Here is the code below.

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};
    });
};

"""output"""

morning 869.5729166666666

result: { "$undefined": true } result (JavaScript): EJSON.parse('{"$undefined":true}')

"""-end of output"""

I'm trying to return morning value which is double but, it returns BSONUndefined. And when I try to reach result from iOS app, I get """ morning: BSONUndefined() """ But before the return statement, it prints the morning value to stitch console correctly.

Ed Bangga
  • 12,879
  • 4
  • 16
  • 30
Ece
  • 51
  • 3

2 Answers2

2

use the return statement before the MongoDB find() query this way

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};
    });
};
vishal gaware
  • 161
  • 2
  • 7
0

you are not written the collection, {which is sending the $undefined:true}

Solution 1 Return collection.find result

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};
    });

Solution 2:

Return the docs itself

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