Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

178
Vistas
ShareDB - Run Database queries in middleware

Currently i am dealing with the following situation:

I have a ShareDB backend up and running in order to realize real time collaboration (text writing).

Every time a user connects i would like to check if the document the user intends to work on exists in the database. If it DOES NOT exist, create it first. If it DOES exist, proceed normally, this should be done in the "connect" middleware:

var backend = new ShareDB();

backend.use('connect', function(context, next) {
  console.log('connect')

  var connection = backend.connect();
  doc = connection.get('collection_name', 'document_id');
  doc.fetch(function(err) {
    if (err) throw err;
    if (doc.type === null) {
      doc.create({content: ''});
      return;
    }
  });

  next()
})

But it triggers an infinite loop, because i trigger an connect action inside the connect middleware.

So i have no idea how i to access the database in the middleware... any idea?

Thanks!

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda