0

I have two seneca services running on port 3000 and 3001. I am trying to create a separate mongoose connection from service on port 3000 create mongoose model and obtain it in service running on port 3001

connection-service.js

require('dotenv').config();
console.log("HOST",process.env.DB_PORT);

const seneca = require('seneca');
const plugins=require('./seneca_plugins/plugins');

const service=seneca();

service.use(plugins.user,{
    collection:{UserAccountsCollection:"users"},
    db:'accounts'
});

service.listen({port:3000,pin:'service:db_connection'});

plugin code for connection-service.js

const UserSchema = require('../db/models/user/user');

module.exports = async function (options) {

    const dbConn = await require('./utils/dbConnections')(process.env.DB_HOST, process.env.DB_PORT, options.db);

    this.add('service:db_connection,usage:GetUserAccountsConnection', async function (args, done) {

        const UserModel = await dbConn.model('UserModel', UserSchema, options.collection.userAccountsCollection);
        console.log(UserModel) //works
        //done(null,{somekey:"someRandom_string_or_object"}) //works
        done(null, {model:UserModel}); //passes empty object i.e. null,{}
    })

}

service.js running as client

const service= require('seneca')();

const plugin=require('./seneca_plugins/plugin');

service.client({port:3000,pin:'service:db_connection'});

service.use(plugin);

plugin code for client service

module.exports = function (options) {

    this.act('service:db_connection,usage:GetUserAccountsConnection', function (msg, reply) {
        console.log("I am acting")
        let model =reply.model   //reply is returned as {}
        console.log(model);    //prints undefined in console

    })

    this.add(..... //other code follows
}

however when I use done(null,{somekey:"someRandom_string_or_object"}) it works but does not works when I pass model created done(null,{model:UserModel})

Udit Bhardwaj
  • 1,761
  • 1
  • 19
  • 29

1 Answers1

0

You can't mix callbacks and async/await. Try https://www.npmjs.com/package/seneca-promisify if you'd like to use async await.

  • mongoose model is being created the problem is that I can't pass anything complex to the callback. Previously I was not using async/await, but that didn't worked, so I tried async/await. I also changed the transport to amqp using seneca-amqp-transport plugin (using rabbitmq) but that also didn't worked. If seneca-promisify plugin can help it would be great if you can provide an example how to use it. My motive is to create a service that can create all the required mongoose connections/models when it is being run and make them available to other services. – Udit Bhardwaj Feb 25 '19 at 14:12