首页 > 解决方案 > FeathersJS 从 1 次 REST 调用将 2 条记录插入 MySql

问题描述

当我使用 .create(item) 方法从客户端(在浏览器中)执行 INSERT 时,我看到 1 个通过 websocket 或 REST 调用到 feathersjs。我看到一个请求进入 Feathersjs。由于未知原因,我看到在 MySql 中创建了 2 行,并且在日志中看到了 2 行: {"message":"after: name_of_service - Method: create","level":"info"}

使用 sequelize 4.42.0 和 feathers-sequelize 6.0.1

从服务器代码中运行 create() 时,仅从客户端运行时我没有问题。

我发现https://github.com/feathersjs-ecosystem/feathers-rethinkdb/issues/80看起来很相似,但适用于不同的数据库,并且解释不适合 MySql。

我出于其他原因切换到 MariaDB,但显然没有任何改变。

我正在使用 FeathersJS v3.x 并升级到 v4.x 以查看是否可以修复它。没有。当我工作时,我一直在制作自己的插入方法,但使用内置方法会很好。

我尝试在 REST 和 websocket 之间切换。

我的钩子:


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

module.exports = {
  before: {
    all: [ ], // authenticate('jwt') normally use, but deactivated to debug
    find: [],
    get: [],
    create: [],
    update: [],
    patch: [],
    remove: []
  },

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

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

服务:

const createService = require('feathers-sequelize');
const createModel = require('../../models/name_of_service.model');
const hooks = require('./name_of_service.hooks');

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

    const options = {
    Model,
    paginate: {
        default: 100,
        max: 2000
    }
    };

    app.use('/name_of_service', createService(options));

    const service = app.service('name_of_service');

    service.hooks(hooks);
};

我希望它在 MySql 表中插入 1 行。但是得到了 2。我预计日志中有一行用于后钩,但看到 2。这已经发生了几个月,我在想,嘿,也许我不是唯一的。

标签: feathersjsfeathers-sequelizefeathers-hook

解决方案


感谢大家的帮助。我发现了这个问题。我通过更改修复了我的菜鸟错误:

app.configure(socketio());
app.configure(socketio(function(io) {io.sockets.setMaxListeners(555);}));

至:

app.configure(socketio(function(io) {io.sockets.setMaxListeners(555);}));


推荐阅读