Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoError: FieldPath cannot be constructed with empty string #10182

Closed
tar-aldev opened this issue Apr 27, 2021 · 6 comments · Fixed by #13249
Closed

MongoError: FieldPath cannot be constructed with empty string #10182

tar-aldev opened this issue Apr 27, 2021 · 6 comments · Fixed by #13249
Labels
developer-experience This issue improves error messages, debugging, or reporting
Milestone

Comments

@tar-aldev
Copy link

tar-aldev commented Apr 27, 2021

Do you want to request a feature or report a bug?
Bug
What is the current behavior?
I am not sure if it is mongoose specific or mongodb specific.
I am getting this error

MongoError: FieldPath cannot be constructed with empty string

Which is, to be honest quite useless. It gives me no infromation about where and how it happened.
I checked my document and it does not have any empty strings at all.

If the current behavior is a bug, please provide the steps to reproduce.

  1. I created a model
  2. used findById() query to retrieve document with populated fields.
  3. then I updated this document by mutating it's fields values (following the documentation)
  4. when I call document.save() I am getting the above error.

Could it be because of .populate? (So it wants Ref not object) But why it says string then? I gues _id is a special datatype and not string.

My document with populate looks like this (some fields omitted)

...
  status: 'private',
  _id: 6082caf6fa074758d394b7e0,
  curriculumId: 5ee31a1f707484c05311d322,
  name: 'TEST 2',
  gradeFromValue: 'grade2',
  gradeToValue: 'grade3',
  teacherId: { // note this is a populated field. Can it cause this error?
    address: null,
    _id: 60811f0ca211d22586a01ec2,
    email: 'john.doe@gmail.com',
    firstName: 'John',
    lastName: 'Doe',
    access: [ [Object] ],
    createdAt: 2021-04-22T07:00:28.799Z,
    updatedAt: 2021-04-22T07:00:28.799Z,
    __v: 0,
    roles: [ 'admin' ],
    id: '60811f0ca211d22586a01ec2'
  },

As supposed, the problem is with populated fields.
.save() will preserve previously populated fields, therefore in my example I tried to populate fields that have been already populated before.

However, could it be possible to somehow improve the error message for this?
the mentioned error will be thrown.

What is the expected behavior?
I should be able to call .save or if there IS an error, it would be really useful to have a more specific message (ideally to have a field name) as now it is nearly impossible to identify what the problem is.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

Node 14
"mongodb": "^3.6.5",
"mongoose": "<=5.10.18",

@Kamikadze4GAME
Copy link
Contributor

@tar-aldev Please, provide script with steps to reproduce. It's too difficult to help without schema, model, query (findById().populate(...)...) etc.

You can omit some fields that don't affect the logic / algorithm.

@vkarpov15 vkarpov15 added the needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity label Apr 29, 2021
@vkarpov15
Copy link
Collaborator

Without code samples or stack trace, it's unlikely we'll be able to repro this issue.

@supipd
Copy link

supipd commented Oct 17, 2021

I have this JSON valid object (pretty print):

a = JSON.parse(`
{
    "vnut_okraj_podmienky": {
        "": {
            "standart_podmienky": {
                "type": "radio",
                "value": "on"
            },
            "nestand_teplota": {
                "type": "number",
                "value": "24"
            },
            "nestand_vlhkost": {
                "type": "number",
                "value": "70"
            }
        }
    }
}
`);

In browser console I can obtain value (=24)
a.vnut_okraj_podmienky[""].nestand_teplota.value
But trying mongosh
db.isover_projects.distinct("vnut_okraj_podmienky.''.nestand_teplota.value")
return [] ... empty list
trying mongosh
db.isover_projects.distinct("vnut_okraj_podmienky..nestand_teplota.value")
error : MongoServerError: FieldPath field names may not be empty strings.
How can I to do it without error ?

@vkarpov15
Copy link
Collaborator

@supipd MongoDB doesn't allow storing empty string as a key name. Nothing Mongoose can do about this unfortunately.

@TheYuriG
Copy link

Not sure if anyone is gonna get to this issue through Google, as I did, but I had this problem happening to me because I forgot a .sort('-') after my .find() and didn't realize it. That was the empty string that the error was screaming about.

@vkarpov15 vkarpov15 reopened this Feb 26, 2023
@vkarpov15 vkarpov15 added developer-experience This issue improves error messages, debugging, or reporting and removed needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity labels Feb 26, 2023
@vkarpov15 vkarpov15 added this to the 6.10.3 milestone Feb 26, 2023
@vkarpov15 vkarpov15 modified the milestones: 6.10.3, 6.10.4, 6.10.5 Mar 10, 2023
@IslandRhythms IslandRhythms linked a pull request Apr 5, 2023 that will close this issue
@venimus
Copy link

venimus commented Feb 28, 2024

I could also add some other case for people that would land here searching google for the same error.
I had a trailing space in my .find(...).populate(<string>), but apparently it is not related to mongoose

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
developer-experience This issue improves error messages, debugging, or reporting
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants