Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

235
Views
¿Por qué sigo recibiendo MongoExpiredSessionError aunque estoy analizando el json en mi función azul?

Con respecto a mi función de Azure, por mi vida no puedo entender por qué MongoExpiredSessionError: Cannot use a session that has ended el error al intentar realizar una inserción extremadamente simple. JSON.parse(result); y JSON.stringify(result); pero sigue recibiendo el mismo error.

También intenté eliminar el bloque " finally ", intenté agregar await delante de collection() pero sigo obteniendo el mismo error. He hecho prácticamente todo lo que he encontrado en la web para solucionar el problema, pero fue en vano.

¿Qué estoy haciendo mal?

Aquí está mi código:

 const mongo = require('mongodb'); const MongoClient = require('mongodb').MongoClient; const uri = "myUri" module.exports = async function (context, req) { const client = await MongoClient.connect(uri, {useNewUrlParser:true}).catch(err => {context.log(err); }); let result = null; let callerEntityProfile = req.body.CallerEntityProfile; let packIdInput = callerEntityProfile.Entity.PackIdInput; let userId = callerEntityProfile.Entity.UserID; try { const db = await client.db("myDB"); let collection = await db.collection("myCollection"); let query = { userId : userId }; result = await collection.findOne(query); let insertPack = { "PackId" : packIdInput }; result.packsOwned.push(insertPack); JSON.parse(result); collection.insertOne(result, function(err, r) { console.log('inside insertOne') console.log(err); // this is where it's showing the error message }); } catch (err) { context.log(err); } finally { client.close(); } }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Dado que no espera hasta el final de la función asincrónica, la acción de cerrar es más rápida que la acción insertarmuchos. Puedes intentar lo siguiente:

 const DB = client.db(databasename); const myAsyncFunction = async() => { const collection = await DB.collection(nameofcollection); // do this INSIDE async function await collection.insertMany(details); // similarly inside async }; client.close();

Alternativamente, puede intentar algo como esto:

 client.connect(err => { if(err) return console.log(err); client.db(databasename).collection(nameofcollection) .then(collection => collection.insertMany(details)) .then(() => client.close()); });

Puede consultar estos hilos SO similares:
Cómo solucionar MongoError: no se puede usar una sesión que ha finalizado
MongoError: no se puede usar una sesión que ha finalizado
Nodo js lanza MongoError
MongoError

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!