MongoDB Realm TypeError: 'forEach' is not a function

I’m getting “TypeError: ‘forEach’ is not a function” In mongo db Raelm. I cannot figure out why. Can someone help?

// This function is the webhook's request handler.
exports = function(payload, response) {
  
    
    const {restaurantsPerPage = 20, page=0} = payload.query;

    let query = {};
    if(payload.query.cuisine) {
        query = {"cuisine": { $eq: payload.query.cuisine } };
    }else if (payload.query.zipcode){
        query = {"address.zipcode": { $eq: payload.query.zipcode } };
    }else if (payload.query.name){
        query = {$text: { $search: payload.query.name } };
    }
    
    const collection = context.services.get("mongodb-atlas").db("sample_restaurants").collection("restaurants");
    let restaurantsList=collection.find(query).skip(page*restaurantsPerPage).limit(restaurantsPerPage).toArray();

    restaurantsList.forEach(restaurant =>{
      restaurant._id = restaurant._id.toString();
    });
    
    let responseData = {
        restaurants : restaurantsList,
        page: page.toString(),
        filters: {},
        entries_per_page: restaurantsPerPage.toString(),
        total_results: restaurantsList.length.toString(),
    };
    
    return  responseData;
};

I assume restaurantsList isn’t an array then. Did you log it out, and if so is it a Promise?

I don’t know Realm, but looking at the docs:

Return Value

The collection.find() action returns a cursor object that points to any documents that match the specified query filters. You can manipulate and access documents in the query result set with the following cursor methods

cursor.toArray()

Iterates the cursor to exhaustion and returns a Promise that resolves to an array that contains all of the iterated documents.

It looks like you can do a .then and iterate example.

Solved the problem . I missed “async” in the first line and “await” in line 16. Code should be as follows,

exports = async function(payload, response) {
  
    
    const {restaurantsPerPage = 20, page=0} = payload.query;

    let query = {};
    if(payload.query.cuisine) {
        query = {"cuisine": { $eq: payload.query.cuisine } };
    }else if (payload.query.zipcode){
        query = {"address.zipcode": { $eq: payload.query.zipcode } };
    }else if (payload.query.name){
        query = {$text: { $search: payload.query.name } };
    }
    
    const collection = context.services.get("mongodb-atlas").db("sample_restaurants").collection("restaurants");
    let restaurantsList= await collection.find(query).skip(page*restaurantsPerPage).limit(restaurantsPerPage).toArray();

    restaurantsList.forEach(restaurant =>{
      restaurant._id = restaurant._id.toString();
    });
    
    let responseData = {
        restaurants : restaurantsList,
        page: page.toString(),
        filters: {},
        entries_per_page: restaurantsPerPage.toString(),
        total_results: restaurantsList.length.toString(),
    };
    
    return  responseData;
};

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.