我有两个 seneca 服务在端口3000
和3001
. 我正在尝试从端口上的服务创建一个单独的猫鼬连接3000
创建猫鼬模型并在端口上运行的服务中获取它3001
连接服务.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'});
插件代码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,{}
})
}
服务.jsrunning as client
const service= require('seneca')();
const plugin=require('./seneca_plugins/plugin');
service.client({port:3000,pin:'service:db_connection'});
service.use(plugin);
客户端服务的插件代码
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
}
但是,当我使用done(null,{somekey:"someRandom_string_or_object"})
它时它可以工作,但当我通过创建的模型时不起作用done(null,{model:UserModel})