SyntaxError: Invalid destructuring assignment target

I am following the “MERN Stack Course - ALSO: Convert Backend to Serverless with MongoDB Realm” video on Youtube from freeCodeCamp.org and can’t get past an error I’m getting. I am using the same code as the video but have a companies database instead of restaurants and different fields. Not sure if maybe there is an issue due to that, but I’m getting an error of :

    filters: null,
             ^^^^
SyntaxError: Invalid destructuring assignment target

The code for the file companiesDAO.js is:

import  mongodb from  "mongodb";

let companies;

export default class CompaniesDAO {
  static async injectDB(conn) {
    if (companies) {
      return;
    }
    try {
      companies = await conn
        .db(process.env.COMPANIES_NS)
        .collection("companies");
    } catch (e) {
      console.error(
        `Unable to establish a connection handle in companiesDAO: ${e}`
      );
    }
  }

  static async getCompanies({
    filters: null,
    page: 0,
    companiesPerPage: 5,
  } = {}) {
    let query 
    if (filters) {
      if ("companyName" in filters) {
        query = { $text: { $search: filters["companyName]"]}}
      } else if ("documentNumber" in filters) {
        query = { "documentNumber": { $eq: filters["documentNumber]"]}}
      } else if ("documentNumber" in filters) {
        query = { "status": { $eq: filters["status]"]}}
      }
    }

    let cursor 

    try {
      cursor = await companies
        .find(query);
    } catch (e) {
      console.error(`Unable to issue find command, ${e}`);
      return {companiesList: [], totalNumCompanies: 0}
    }

    const displayCursor = cursor.limit(companiesPerPage).skip(companiesPerPage * page);
    
    try {
      const companiesList = await displayCursor.toArray();
      const totalNumCompanies = await companies.countDocuments(query);
      
      return {companiesList, totalNumCompanies}
    } catch (e) {
      console.error(`Unable to convert cursor to array or problem counting documents`);
      return {companiesList: [], totalNumCompanies: 0}
    }
  }
}

I can include code for the other files if needed. Any ideas?

From just this information, it looks like the problem is that at some point in the code there is a destructuring assignment referencing filters. The value null cannot be destructured.

Thanks! The only other page with “filters” code is the controller file:

import CompaniesDAO from "../dao/companiesDAO.js";

export default class CompaniesController {
  static async apiGetCompanies(req, res, next) {
    const companiesPerPage = req.query.companiesPerPage
      ? parseInt(req.query.companiesPerPage, 5)
      : 20;
    const page = req.query.page ? parseInt(req.query.page, 5) : 0;

    let filters = {};
    if (req.query.documentNumber) {
      filters.documentNumber = req.query.documentNumber;
    } else if (req.query.status) {
      filters.status = req.query.status;
    } else if (req.query.companyName) {
      filters.companyName = req.query.companyName;
    }

    const { companiesList, totalNumCompanies } =
      await CompaniesDAO.getCompanies({
        filters,
        page,
        companiesPerPage,
      });

    let response = {
      companies: companiesList,
      page: page,
      filters: filters,
      entries_per_page: companiesPerPage,
      total_results: totalNumCompanies,
    };
    res.json(response);
  }
}

Nothing I’ve tried has worked so far. If there’s nothing further to try I’ll set up another database with the exact code from the video and mongodb database setup and see if I can get that working and go from there.

What happens if instead of initializing filters to null, you use and empty object?

I tried that and I get the same error but for the next item:

    page: 0,
          ^

SyntaxError: Invalid destructuring assignment target

If I initialize everything to an empty object, I can start the server, but when I go to http://localhost:5000/api/v1/companies, I get a “this site cannot be reached, localhost refused to connect” page and the error in the console is:

if (filters) {
    ^

ReferenceError: filters is not defined

Looks like there is an issue with my setup , I created a new project and followed the video exactly and got to where I was before and was able to get the data from the database.

I did start the initial project on a Next.js app I am building so there’s probably an issue there. Any info on possible conflicts in creating a backend like in the “MERN Stack Course - ALSO: Convert Backend to Serverless with MongoDB Realm” but on Next.js would be greatly appreciated!

Sorry I wasn’t more help. This sort of problem can be really tricky to track down.

No worries, I should have looked for a Next.js specific resource. Thankfully I was able to find a " How to Integrate MongoDB Into Your Next.js App" how-to directly from Mongodb, I’m hoping that’ll do the trick!

Good luck and happy coding!

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