Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch/Remove methods mess up Find method #313

Closed
boogdanpop opened this issue Jul 26, 2019 · 1 comment
Closed

Patch/Remove methods mess up Find method #313

boogdanpop opened this issue Jul 26, 2019 · 1 comment

Comments

@boogdanpop
Copy link

boogdanpop commented Jul 26, 2019

Steps to reproduce

I'm trying to build feathers-chat-angular with Sequelize and I encountered some issues.
/~https://github.com/feathersjs-ecosystem/feathers-chat-angular

All good when creating a user, authenticate, adding or displaying the messages, but when I try to edit or delete a message, the messages list disappear (when deleting) or the messages list has only the edited message in it.

Here is the code:

FE

chat.component.html

<button type="button" (click)="editMessage(message)"> Edit</button> <button type="button" (click)="deleteMessage(message)"> Delete</button>
chat.component.ts
editMessage(message) {
this.chatService.editMessage(message._id, {text: 'This message was edited'});
}
deleteMessage(message) {
this.chatService.deleteMessage(message.id);
}

chat.service.ts

editMessage(id: number, body: object) {
if (!id) {
return;
}
// feathers-reactive Observables are hot by default,
// so we don't need to subscribe to make create() happen.
this.feathers
.service('messages')
.patch(id, body);
}

deleteMessage(id: number) {
if (!id) {
return;
}
// feathers-reactive Observables are hot by default,
// so we don't need to subscribe to make create() happen.
this.feathers
.service('messages')
. .remove(id);
}
feathers.service.ts

`import { Injectable } from '@angular/core';

import * as feathersRx from 'feathers-reactive';
import * as io from 'socket.io-client';

import feathers from '@feathersjs/feathers';
import feathersSocketIOClient from '@feathersjs/socketio-client';
import feathersAuthClient from '@feathersjs/authentication-client';

/**

  • Simple wrapper for feathers
    */
    @Injectable()
    export class Feathers {
    private _feathers = feathers(); // init socket.io
    private _socket = io('http://localhost:3030'); // init feathers

constructor() {
this._feathers
.configure(feathersSocketIOClient(this._socket)) // add socket.io plugin
.configure(feathersAuthClient({ // add authentication plugin
storage: window.localStorage
}))
.configure(feathersRx({ // add feathers-reactive plugin
idField: '_id'
}));
}

// expose services
public service(name: string) {
return this._feathers.service(name);
}

// expose authentication
public authenticate(credentials?): Promise {
return this._feathers.authenticate(credentials);
}

// expose logout
public logout() {
return this._feathers.logout();
}
}
`

BE
messages.model.js

`// See http://docs.sequelizejs.com/en/latest/docs/models-definition/
// for more of what you can do here.
const Sequelize = require('sequelize');
const DataTypes = Sequelize.DataTypes;

module.exports = function (app) {
const sequelizeClient = app.get('sequelizeClient');
const messages = sequelizeClient.define('messages', {
text: {
type: DataTypes.STRING,
allowNull: false
}
}, {
hooks: {
beforeCount(options) {
options.raw = true;
}
}
});

// eslint-disable-next-line no-unused-vars
messages.associate = function (models) {
// Define associations here
// See http://docs.sequelizejs.com/en/latest/docs/associations/
messages.belongsTo(models.users);
};

return messages;
};
`

message.service.js

// Initializes the messagesservice on path/messages`
const createService = require('feathers-sequelize');
const createModel = require('../../models/messages.model');
const hooks = require('./messages.hooks');

module.exports = function (app) {
const Model = createModel(app);
const paginate = app.get('paginate');

const options = {
Model,
paginate
};

// Initialize our service with any options it requires
app.use('/messages', createService(options));

// Get our initialized service so that we can register hooks
const service = app.service('messages');

service.hooks(hooks);
};
`

message.hooks.js

`const { authenticate } = require('@feathersjs/authentication').hooks;

module.exports = {
before: {
all: [ authenticate('jwt') ],
find: [includeUser],
get: [includeUser],
create: [],
update: [],
patch: [],
remove: []
},

after: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
},

error: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
}
};

function includeUser(hook) {
const sequelize = hook.app.get('sequelizeClient');
const { users } = sequelize.models;

hook.params.sequelize = {
include: [users],
};

return hook;
}
`

Expected behavior

The deleted message is removed from the list (and from DB), and then the messages are updated in the list. The edited message is edited and the list is updated.

Actual behavior

The deleted message is removed from the list (and from DB), but the list is empty. The edited message is edited the list contains only the currently edited message.

System configuration

"@feathersjs/authentication": "^2.1.16"
"@feathersjs/authentication-jwt": "^2.0.10"
"@feathersjs/authentication-local": "^1.2.9"
"@feathersjs/configuration": "^2.0.6"
"@feathersjs/errors": "^3.3.6"
"@feathersjs/express": "^1.3.1"
"@feathersjs/feathers": "^3.3.1"
"@feathersjs/socketio": "^3.2.9"
"compression": "^1.7.4"
"cors": "^2.8.5"
"feathers-sequelize": "^4.1.1"
"helmet": "^3.20.0"
"mysql2": "^1.6.5"
"sequelize": "^6.0.0"
"serve-favicon": "^2.5.0"
"winston": "^3.2.1"

NodeJS version:
10.15.3
Browser Version:
Chrome Version 75.0.3770.142

@daffl
Copy link
Member

daffl commented Jul 27, 2019

Your idField: '_id' configuration is probably wrong. Sequelize uses id by default so it should be idField: 'id'. Closing since this is not a feathers-sequelize problem.

@daffl daffl closed this as completed Jul 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants