简体   繁体   中英

How to add more parameters on feathersjs service method with primus

I checked this link https://docs.feathersjs.com/real-time/primus.html to setup additional parameters on service method but I didn't figure out how to do it.

Below is my service class:

create(id, shellId, params) {
     ...

  }

below is the primus configuration:

 app.configure(primus({
    transformer: 'websockets',
    timeout: false
  }, (primus) => {
    primus.library();
    primus.save(path.join(__dirname, '../public/dist/primus.js'));
  }))

In the manual, it mentions something like below:

primus.use('todos::create', function(socket, done){
    // Exposing a request property to services and hooks
    socket.request.feathers.referrer = socket.request.referrer;
    done();
  });

but I am not sure what this function is doing. And also I tried to add above code in my application, I will get socket.request is undefined error. How to add more parameters on service class in this case?

You can only use the official service methods and their signatures, in the case of create it is data for the entire data object and params for additional parameters. Your service class create looks something like this:

class MyService {
  create(data, params) {
    const { shellId, id } = data;
    // Do something here

    return Promise.resolve({ /* some data here */ });
  }  
}

app.use('/myservice', new MyService());

On the client you can use it through the Feathers client or a direct Primus connection like this:

primus.send('myservice::create', {
  id: 'test',
  shellId: 'testing'
}, (error, data) => {
  console.log('Returned data', data);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM