TypeError: RecordsRemover is not a constructor (forest-admin-mongoose)

Expected behavior

Should remove multiple records: https://docs.forestadmin.com/documentation/v/v6/reference-guide/routes/default-routes#delete-a-list-of-records

Actual behavior

From the code stated in the above documentation link, I get TypeError: RecordsRemover is not a constructor

const express = require('express');
const { PermissionMiddlewareCreator, 
  RecordCreator,
  RecordUpdater, 
  RecordRemover,
  RecordsRemover, 
  RecordsGetter } = require('forest-express-mongoose');
const { outfitProductsCollection } = require('../models');
const mongoose = require('mongoose');

const router = express.Router();
const permissionMiddlewareCreator = new PermissionMiddlewareCreator('outfitProductsCollection');

/* The others routes*/

// Delete a list of outfits
router.delete('/outfitProductsCollection', permissionMiddlewareCreator.delete(), (request, response, next) => {
  // Learn what this route does here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/routes/default-routes#delete-a-list-of-records
  new RecordsGetter(outfitProductsCollection).getIdsFromRequest(request)
  .then((ids) => {
    const recordsRemover = new RecordsRemover(ids);
    recordsRemover.perform(request)
      .then(() => response.status(204).send())
      .catch(next);
  })
  .catch(next);
});

Failure Logs

[start] DELETE /forest/outfitProductsCollection 500 283 - 12.286 ms
[start] TypeError: RecordsRemover is not a constructor
at /Users/emixam23/Workspace/forest-admin/eyesapp/routes/outfit-products-collection.js:71:28

Context

Please provide any relevant information about your setup.

  • Project Name: EyesApp
"dependencies": {
    "chalk": "~1.1.3",
    "cookie-parser": "1.4.4",
    "cors": "2.8.5",
    "debug": "~4.0.1",
    "dotenv": "~6.1.0",
    "express": "~4.16.3",
    "express-jwt": "5.3.1",
    "forest-express-mongoose": "^6.0.0",
    "mongoose": "~5.8.2",
    "morgan": "1.9.1",
    "npm-watch": "^0.6.0",
    "require-all": "^3.0.0"
  }

I am using the latest version of ForestAdmin (setup from the website using Docker)

Hi :wave: @Thomas welcome to our community.
How did you import the RecordsRemover?

const { RecordsRemover } = require('forest-express-sequelize');

Hey!

Updated the question, sorry for the missing imports :slight_smile:

Note: forest-express-sequelize is for SQL, but we’re currently on MongoDB so we use forest-express-mongoose

Ok so, the import looks good.
Which version of forest-express-mongoose do you use?

Question updated with the package json :slight_smile:

It’s seem to have a missed export on our end :scream:.
We will work on a fix soon.
But you can do this to achieve the wanted behavior.

const { ResourcesRemover } = require('forest-express-mongoose');

const recordsRemover = new ResourcesRemover(outfitProductsCollection, ids);
recordsRemover.perform()
  .then(() => response.status(204).send())
  .catch(next);;

Hope it’s help

2 Likes

Alright, that what I thought. Thanks for your feedback, can you update me once you release the export of RecordsRemover please?

Thanks! :slight_smile:

1 Like

Hi @Thomas,

We just released a new version of forest-express-mongoose (and forest-express-sequelize) that exposes the RecordsRemover service. You will have to update your dependency to latest version (i.e: 6.2.1):

# Using yarn
yarn add forest-express-mongoose@latest
# Or with NPM
npm i forest-express-mongoose@latest

Then you can update your delete route. I just tried on one of my local projects with this code and it works (using a users model):

// Delete a list of Users
router.delete('/users', permissionMiddlewareCreator.delete(), (request, response, next) => {
  new RecordsGetter(users).getIdsFromRequest(request)
    .then((ids) => {
      const recordsRemover = new RecordsRemover(users);
      recordsRemover.remove(ids)
        .then(() => response.status(204).send())
        .catch(next);
    })
    .catch(next);
});

Side note: currently, there are a some errors in the documentation about this method, it will be fixed soon! Lines 17 to 20 should be updated since we have to use remove instead of perform:

const recordsRemover = new RecordsRemover(companies);
recordsRemover.remove(ids)
  .then(() => response.status(204).send())
  .catch(next);

Edit: Documentation is now up-to-date.

Let us know if it fixes your problem!

2 Likes