Understanding query in app functions

Hello I am quite new to this tool. I am trying to intake a query to perform a search in the atlas app, but am not sure why my code won’t understand the queries given. I am quite new to programming so any help with details would be greatly appreciated

// This function is the endpoint’s request handler.
exports = async function(payload, response) {

    query = JSON.stringify(payload.query); //prints out fine
    console.log(payload.query.type); //prints out undefined [object Object]
    
    if(payload.query.type){
      //prints "message":"Cannot access member 'type' of undefined","name":"TypeError"}
    }
    
    const collection = context.services.get("mongodb-atlas").db("portfolio").collection("blogdatas");
    let postList = await collection.find().toArray();

    postList.forEach(blogdata => {
      blogdata._id = blogdata._id.toString();
    })

    let responseData = {
        blogdatas: postList,
        query:query,
        totalNum: postList.length.toString()
    }

return responseData;
};

console.log(payload.query); // returns prints out undefined [object Object]

I’m pretty new too, but I think I can answer this…

Your code assumes that your “payload” parameter document has a key “query”, which contains a document that has a key “type”.

If you invoke the function from the console, this should work:

exports({“query”: {“type”: “some value for type”}})

1 Like