feathersjs / feathers

The API and real-time application framework
https://feathersjs.com
MIT License
15.05k stars 751 forks source link

Cannot remove item from mongodb #2050

Closed coderinblack08 closed 4 years ago

coderinblack08 commented 4 years ago

Extended from issue: https://github.com/feathersjs/feathers/issues/2044

Error Message

Unhandled run time error
NotFound: No record found for id

Client Code

const getStar = await $store.dispatch('stars/find', {
    query: {
        contest_id: $route.params.id,
        user_id: $store.state.auth.user._id,
    },
});
 await $store.dispatch('stars/remove', getStar.data[0]._id);

Backend hook

import { ObjectID } from 'mongodb';

// my before remove hook
async (context: any): Promise<any> => {
    context.id = new ObjectID(context.id);
    console.log(context);
    return context;
},

My thoughts

Screen Shot 2020-08-28 at 4 03 38 PM

It seems that the query _id and the id property are different... I don't think this is the problem because the id property is the correct id and it is anything this:

Screen Shot 2020-08-28 at 4 05 04 PM

Resources looked at https://docs.feathersjs.com/help/faq.html#why-should-i-use-feathers, https://github.com/feathersjs-ecosystem/feathers-mongodb#querying

marshallswain commented 4 years ago

Have you tried converting the id to an object ID?

coderinblack08 commented 4 years ago

yes, that is what the hook is supposed to do :)

marshallswain commented 4 years ago

Sorry. I missed the hook on my first read.

marshallswain commented 4 years ago

I recommend setting a debugger or breakpoint in the remove method of feathers-mongodb and see what's actually making it into the adapter.

coderinblack08 commented 4 years ago

I'm not quite familiar with the vscode debugging tool, may you elaborate or provide a working example if possible? That would be awesome.

coderinblack08 commented 4 years ago

It seems that the error says id, not _id? Is this a problem?

coderinblack08 commented 4 years ago
import { NullableId, Params } from '@feathersjs/feathers';
import { Service, MongooseServiceOptions } from 'feathers-mongoose';
import { model } from 'mongoose';
import { Application } from '../../declarations';

export class Stars extends Service {
  //eslint-disable-next-line @typescript-eslint/no-unused-vars
  constructor(options: Partial<MongooseServiceOptions>, app: Application) {
    super(options);
  }
  async remove(id: NullableId, params: Params) {
    const removedStar = await model('stars').deleteOne({ _id: id });
    return removedStar;
  }
}

I overwrote the service, it seems like a hack, is there a better way?

marshallswain commented 4 years ago

The better way is for it to just work, but we don't know why it's not working in your case. This override should work just fine.

SmartArray commented 4 years ago

Interestingly we are experiencing the same issue with NeDB. Investigations are running...

SmartArray commented 4 years ago

I don't think this is the case with your MongoDB driver, but I stumbled across something interesting. So this is for reference purposes only:

The package @feathers-nedb expands the deletion request to this NeDB query: $and: [ { undefined: 'StiJfdsHlnNtkqNX' } ]

Of course it won't find a document with this query.

EDIT: Please check if that's the case for you too by placing a console.log after this line: https://github.com/feathersjs-ecosystem/feathers-mongodb/blob/6bf168269f7cbb9f329cc8a530d5ee7677b645f7/lib/index.js#L46