"message":"Cannot access member 'resultsPerPage' of undefined","name":"TypeError"

I am new to MongoD trying to learn realm. I just tried to create an HTTP Endpoint through this function (I’ve attached the screenshot below), I can’t understant the error when I have already defined it.

Thanks in advance.

did you get an answer to this question I am getting the same error, following along with a tutorial and no idea what I need to do.

Hi @Booby_Trap & @Jordan_Enwright,

Apologies for the late reply: we’ve been trying a similar function, as below:

// This function is the endpoint's request handler.
exports = async function(request, response) {
    // Data can be extracted from the request as follows:

    // Query params, e.g. '?arg1=hello&arg2=world' => {arg1: "hello", arg2: "world"}
    const {resultsPerPage = 20, page = 0} = request.query;

    // Headers, e.g. {"Content-Type": ["application/json"]}
    const contentTypes = request.headers["Content-Type"];

    // Raw request body (if the client sent one).
    // This is a binary object that can be accessed as a string using .text()
    const reqBody = request.body;

    console.log(`resultsPerPage => ${resultsPerPage}, page => ${page}`);
    console.log("Content-Type:", JSON.stringify(contentTypes));
    console.log("Request body:", reqBody);
    
    return  {success: true};
};

and that works perfectly well, so it’s difficult to understand what may be wrong in your implementations without more context (for example, HTTP endpoint settings, Authorization, etc.).

Can you please post, in textual format (screenshots don’t help much here), the function that fails in as few steps as possible, including the additional information about the endpoint configuration?

// This function is the webhook's request handler.
exports = async function(payload, response) {
  
  const id = payload.query.id || ""

  const restaurants = context.services.get("mongodb-atlas").db("sample_restaurants").collection("restaurants");

  const pipeline = [
    {
        $match: {
            _id: BSON.ObjectId(id),
        },
    },
          {
              $lookup: {
                  from: "reviews",
                  let: {
                      id: "$_id",
                  },
                  pipeline: [
                      {
                          $match: {
                              $expr: {
                                  $eq: ["$restaurant_id", "$$id"],
                              },
                          },
                      },
                      {
                          $sort: {
                              date: -1,
                          },
                      },
                  ],
                  as: "reviews",
              },
          },
          {
              $addFields: {
                  reviews: "$reviews",
              },
          },
      ]
      
      restaurant = await restaurants.aggregate(pipeline).next()
      restaurant._id = restaurant._id.toString()
      
      restaurant.reviews.forEach(review => {
        review.date = new Date(review.date).toString()
        review._id = review._id.toString();
      });
  return restaurant
};

I am following a “FreeCodeCamp” tutorial but am paying for mongodb and want to turn this into a final project for school

i get this error:

 error: 
{"message":"Cannot access member 'id' of undefined","name":"TypeError"}

Hi @Jordan_Enwright,

That doesn’t look like is the same as the OP issue: the following runs without errors:

exports = async function (payload, response) {
  const reqId = payload.query.id || ""
  
  console.log(`Request: ${EJSON.stringify(payload)}`);
  console.log(`Requested id: ${reqId}`);
  
  const restaurants = context.services.get("mongodb-atlas").db("sample_restaurants").collection("restaurants");

  const pipeline = [
    {
      $match: {
        _id: BSON.ObjectId(reqId),
      },
    },
    {
      $lookup: {
        from: "reviews",
        let: {
          id: "$_id"
        },
        pipeline: [
          {
            $match: {
              $expr: {
                $eq: ["$restaurant_id", "$$id"]
              },
            },
          },
          {
            $sort: {
              date: -1
            },
          },
        ],
        as: "reviews"
      },
    },
    {
      $addFields: {
        reviews: "$reviews"
      },
    },
  ]

  restaurant = await restaurants.aggregate(pipeline).next();
  
  console.log(`Restaurant: ${EJSON.stringify(restaurant)}`);
  
  restaurant._id = restaurant._id.toString()

  restaurant.reviews.forEach(review => {
    review.date = new Date(review.date).toString()
    review._id = review._id.toString();
  });
  
  return restaurant
};

(assuming the sample_restaurants database is around, of course)

Can you please test it in your environment?


Facing similar error please help with this.

Thanks in advance.

Hi @Vipul_S_Patil,

This is certainly an issue in your setup: while I can’t refer directly to your app, as this is a public forum, I’ll try to explain a couple of things that needs to be corrected.

  • You’re trying to access the username and password by their values: this is correct in principle, but you’re calling it the wrong way! The only values in your setup are Atlaspublickey and Atlasprivatekey, so the two assignments there make no sense, the username should be obtained by calling context.values.get('Atlaspublickey')
  • The password is a bit more convoluted: this is obviously a Secret value, but, you can’t access it directly with context.values.get('Atlasprivatekey'). You have to expose the access via a normal, non-Secret value that links to the Secret itself.

Once you’ve done that, we can diagnose if and what else would need corrections.

Thanks… Let me do the Required setup and check.


Facing same issue. Please check.