Getting error message with feathers-chat tutorial using typescript "Error: An id must be provided to the 'users.get' method"

Viewed 435

I'm following the feathers chat tutorial on the feathers guide page (https://docs.feathersjs.com/guides/basics/generator.html#generating-the-application) and the application is failing on one of the tests.

**OBS: I'm following the typescript version but using mongoose for connecting to MongoDB **

Here's the error message

1) 'messages' service
   creates and processes message, adds user information:
 Error: An id must be provided to the 'users.get' method
  at Object.validate (node_modules/@feathersjs/feathers/lib/hooks/base.js:23:11)
  at /Users/fabriciobracht/Documents/cpsc2650/feathers-chat/node_modules/@feathersjs/commons/src/hooks.ts:132:47
  at processTicksAndRejections (internal/process/task_queues.js:93:5)
  at addUser (src/hooks/populate-user.ts:13:20)
  at Object.<anonymous> (src/hooks/populate-user.ts:28:24)
  at Context.<anonymous> (test/services/messages.test.ts:20:21)

The output log contains basically the same information. So, going backwards, the error is generated when this line in the test runs (messages.test.ts:20:21)

    const params = { user };
    const message = await app.service('messages').create({
      text: 'a test',
      additional: 'should be removed'
    }, params);

Which calls populate-user.ts

export default (): Hook => {
  return async (context: HookContext) => {
    // Get `app`, `method`, `params` and `result` from the hook context
    const { app, method, result, params } = context;
    // Function that adds the user to a single message object
    const addUser = async (message: any) => {
      // Get the user based on their id, pass the `params` along so
      // that we get a safe version of the user data
      const user = await app.service('users').get(message.userId, params);

      // Merge the message content to include the `user` object
      return {
        ...message,
        user
      };
    };

    // In a find method we need to process the entire page
    if (method === 'find') {
      // Map all data to include the `user` information
      context.result.data = await Promise.all(result.data.map(addUser));
    } else {
      // Otherwise just update the single result
      context.result = await addUser(result);
    }

    return context;
  };
}

But this is where I get lost. Can someone shed some light on the matter? Thx

0 Answers
Related