denodrivers / mongo

🍃 MongoDB driver for Deno 🦕
https://deno.land/x/mongo
MIT License
514 stars 95 forks source link

Error when insertOne #375

Closed avalero closed 2 years ago

avalero commented 2 years ago

Hello, if I try to insertOne with this Document

{
    "name": "Colegio Velazquez",
    "address": "Pl. Mayor, 2",
    "population": "Madrid",
    "phone": "917511520",
    "email": "velazquez@gmail.com",
    "type":["ACADEMY","CAMPUS"],
    "nature": "PUBLIC",
    "languages": ["español", "ingles", "aleman"],
    "contacts":[{"name":"Pedro", "email":"pedro@gmail.com", "phone":"333333333"}]
 }

I get the following error

api_bo_1    | error: Uncaught (in promise) TypeError: Cannot convert object to primitive value
api_bo_1    |         throw new BSONTypeError(`Unrecognized or invalid BSON Type: ${value}`);
api_bo_1    |                                                                       ^
api_bo_1    |     at serializeInto (https://deno.land/x/web_bson@v0.2.4/src/parser/serializer.ts:891:71)
api_bo_1    |     at serializeObject (https://deno.land/x/web_bson@v0.2.4/src/parser/serializer.ts:392:20)
api_bo_1    |     at serializeInto (https://deno.land/x/web_bson@v0.2.4/src/parser/serializer.ts:1131:17)
api_bo_1    |     at serializeObject (https://deno.land/x/web_bson@v0.2.4/src/parser/serializer.ts:392:20)
api_bo_1    |     at serializeInto (https://deno.land/x/web_bson@v0.2.4/src/parser/serializer.ts:878:17)
api_bo_1    |     at serializeObject (https://deno.land/x/web_bson@v0.2.4/src/parser/serializer.ts:392:20)
api_bo_1    |     at serializeInto (https://deno.land/x/web_bson@v0.2.4/src/parser/serializer.ts:1131:17)
api_bo_1    |     at serialize (https://deno.land/x/web_bson@v0.2.4/src/bson.ts:100:30)
api_bo_1    |     at https://deno.land/x/mongo@v0.31.0/src/protocol/message.ts:34:24
api_bo_1    |     at Array.map (<anonymous>)

Inserting that document directly from mongo shell results OK

The error is whith the objects array contacts as inserting

{
    "name": "Colegio Velazquez",
    "address": "Pl. Mayor, 2",
    "population": "Madrid",
    "phone": "917511520",
    "email": "velazquez@gmail.com",
    "type":["ACADEMY","CAMPUS"],
    "nature": "PUBLIC",
    "languages": ["español", "ingles", "aleman"],
    "contacts":[]
 }

works just fine

avalero commented 2 years ago

Testing I discovered the reason is because it expects and _id for any object (document), which should not be required (as it is not required by mongo).

This insertOne works just fine

{
    "name": "Colegio Velazquez",
    "address": "Pl. Mayor, 2",
    "population": "Madrid",
    "phone": "917511520",
    "email": "velazquez@gmail.com",
    "type":["ACADEMY","CAMPUS"],
    "nature": "PUBLIC",
    "languages": ["español", "ingles", "aleman"],
    "contacts":[{"_id": new ObjectId(), "name":"Pedro", "email":"pedro@gmail.com", "phone":"333333333"}]
 }
avalero commented 2 years ago

Closed because I cannot reproduce this with other inputs, there must be something on my input that makes it not work.