I have a simple POST API in Next.js to insert a document into a collection. When I make the request with Postman, I get a 200 status code and acknowledged: true, but the document is not saved in the collection.
Post Function:
//…
const stringa = {"description": "prova testo fisso"}
;
const jsontext = JSON.parse(stringa);
const result = await collection.insertOne(jsontext);
//…
Console Log:
Inizio Post
{ description: ‘prova testo fisso’ }
Risultato inserimento: {
acknowledged: true,
insertedId: new ObjectId(‘67116edf8a9c019f4fbe087b’)
}
POST /api/cdc 200 in 2084ms
Are you sure it’s inserting into the correct collection?
Yes i’m sure.
If that weren’t the case, it should give me an error or create a new collection with the value, I believe.
I have another function that reads data with a GET request and works correctly, so the connection to the database and the selection of the collection are correct.
This is the get funciont:
const client = await clientPromise;
const db = client.db(“expManager”);
const cdcs = await db
.collection(“cdc”)
.find({})
.sort({ metacritic: -1 })
.toArray();
return Response.json(cdcs)
and this is the post function:
const client = await clientPromise;
const db = client.db(‘expManger’);
const collection = db.collection(‘cdc’);
const stringa = {"description": "prova testo fisso"}
;
const jsontext = JSON.parse(stringa);
console.log(jsontext);
const result = await collection.insertOne(jsontext);
Mongo will create a collection (and database) if the target does not exist, obviously given user permissions without notification.
From your logs originally it does seem that mongo is reporting a success return code and giving you the ObjectID of the document that’s been inserted.
From the last code fragment, I cannot see anything wrong, maybe someone else can see something that I seem to be missing. If you check with Compass can see you see anything strange?
I am not sure I understand well.
1 - You have insert API that is tested with postman and you receive acknowledgement and the inserted id. As understood by
Then, I assume that insertOne does work correctly.
2 - You have API code that reads back correctly from the same connection/database/collection as mentioned by
Then, I assume that insertOne does work correctly.
Since insertOne must be working correctly, then either you are not looking at the right place to see if the data is saved or the data disappear some how.
1 - What do you use to verify that the data is not saved?
2 - What version of mongod are you running and where it is running? An atlas cluster or local deployment?
3 - What else is done between the time you test the API with postman and the time you verify that the data is saved?
One possible reason why documents are removed automatically would be the presence of a TTL index. But your sample document have no timestamp or date field, so it could not be the case.
If you are not using Atlas, could you try to create an M0 cluster and use that for your tests.
Please read Formatting code and log snippets in posts so that the next time you publish code it is more readable.
1 - You have insert API that is tested with postman and you receive acknowledgement and the inserted id. As understood by
YES
2 - You have API code that reads back correctly from the same connection/database/collection as mentioned by
MY GET READS CORRECT DATA ALREADY IN THE COLLECTION BUT NOT LIST DATA INSERTED BY THE POST ABOVE.
3 - What else is done between the time you test the API with postman and the time you verify that the data is saved?
One possible reason why documents are removed automatically would be the presence of a TTL index. But your sample document have no timestamp or date field, so it could not be the case.
NOTHING. I SEND POST WITH POSTMAN AND I SEND GET ALWAYS WITH POSTMAN. I’VE TRIED TO DO THE SAME INSERT DIRECTLY WITH COMPASS (WITHOUT ID, ONLY WITH KEY DESCTIPTION) AND THE DOCUMENT IS CORRECTLY SAVED ON COLLECTION.
SORRY, IM USE LOCAL MONGO DB LATEST VERSION.
Thanks!!
Hello @Daniele_Poletti, welcome to the community forums!
Without having much context on the rest of your application, I see one thing in the code provided that might be confusing your app: the JSON parse function.
Have you tried simplifying your code to:
const db = client.db(‘expManger’);
const collection = db.collection(‘cdc’);
const doc = {"description": "prova testo fisso"};
const result = await collection.insertOne(doc);
console.log(`New document with _id: ${result.insertedId}`)
Please check your keyboard. It looks like your Caps Lock is on.
Perhaps it is a simple typing error:
You have the following for the GET
vs
the following for the POST
So it looks like John_Sewell was on the right track with:
Thanks!!! I’m an idiot!!! It was only a typing error on the db name.
Compass don’t refresh db list automatically and i haven’t seen the new db with the wrong name!
I have found a new db with a collection named cdc with dozens documents…
Sorry for caps lock… It was just to distinguish your questions from my answers.
Yep, that has hurt me a couple of time too.
Ha, I was staring at that code fragment and completely missed the typo…and I was looking for one! That has also got me a few times as well…having to go play hunt the document…
This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.