Mongo DB insertOne Error

I am inserting a local data to database

bh.local.bookData = {
    isbn: 'OL7353617M',
    title: 'Fantastic Mr. Fox',
    author: 'Roald Dahl',
    publishedYear: 1988
  },

got this error

TypeError: Reflect.get called on non-object
    at Reflect.get (<anonymous>)
    at Collection.insertOne (/Users/jeevan/neutrinos-studio/jk-book-management-backend/server/node_modules/mongodb/lib/collection.js:147:32)
    at MongoPersistance.insertOne (file:///Users/jeevan/neutrinos-studio/jk-book-management-backend/server/src/utils/ndefault-mongodb/Mongodb/MongoPersistance.ts:86:46)
    at books.insertbook (file:///Users/jeevan/neutrinos-studio/jk-book-management-backend/server/src/sd-services/books.ts:329:75)
    at books.dfsdfd (file:///Users/jeevan/neutrinos-studio/jk-book-management-backend/server/src/sd-services/books.ts:318:29)
    at books.ifInvalidIsbn (file:///Users/jeevan/neutrinos-studio/jk-book-management-backend/server/src/sd-services/books.ts:298:33)
    at books.validateIsbn (file:///Users/jeevan/neutrinos-studio/jk-book-management-backend/server/src/sd-services/books.ts:286:29)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async file:///Users/jeevan/neutrinos-studio/jk-book-management-backend/server/src/sd-services/books.ts:89:22

the same thing works properly in insertMany (after inserting bookData into a Array) but insertOne gives this error

Hello @JeevanKumar ,Ensure that the database is initialized properly, the collections are correct, and the fields are properly defined.

@Sam

database initialised properly because it works for other operations like insertMany and find.

Hi @JeevanKumar
Thanks for sharing the details and the screenshot :raising_hands:

The issue here is with the Options field in your insertOne config. For insertOne, options are meant only for things like session, writeConcern, etc. β€” but right now you have it mapped to bh.input.body.isbn, which makes MongoDB treat it as the options object and throws the Reflect.get error.

Can you please try removing the Options mapping (leave it blank), and just keep:

  • Collection β†’ books
  • Operation β†’ insertOne
  • Document β†’ bh.local.bookData
  • Result Mapping β†’ bh.local.response.body

That should fix the error. Give it a try and let us know if it works

1 Like

Thank you @Shamnad_KS
the option was the problem.

but we can’t keep the option blank . it shows required .

it worked when I put a null value like this