查询,url 和 body 参数在 seneca 和 seneca-web 中没有正确合并

Query,url and body parameters not merged correctly in seneca with seneca-web

我在使用 seneca 和 seneca-web 时遇到了麻烦。我真的是初学者。

到目前为止,这是我的代码:

"use strict";

var express = require('express');
var Web = require("seneca-web");
var bodyParser = require('body-parser')

var plugin = require('./products_actions/products_actions');

module.exports = plugin;

var entities = require('seneca-entity')
var seneca = require('seneca')();

seneca.use(plugin);
seneca.use(entities);

seneca.use('mysql-store', {
    name : 'ecrm',
    host : 'localhost',
    user : 'root',
    password : 'ecrm',
    port : 3306
})

seneca.ready(function(err) {


    var Routes = [ {
        pin : 'area:product,action:fetch,criteria:*',

        prefix : '/products/fetch',

        map : {
            byId : {
                GET : true,
                suffix : "/:id"
            }
        }

    }, {
        pin : 'area:product,action:*',

        prefix : '/products',

        map : {
            fetch : {
                GET : true
            },

            add : {
                GET : false,
                PUT : true
            }
        }

    } ];

    var app = express();
    app.use(bodyParser.json());

    var config = {
        routes : Routes,
        adapter : require('seneca-web-adapter-express'),
        context : app,
        options : {
            parseBody : false
        }
    }

   seneca.use(Web, config);

   app.listen(3000);
});

这是定义塞内卡动作的代码:

module.exports = function(options) {
var seneca = this;

// ADD
seneca.add({
    area : "product",
    action : "add"
}, function(req, done) {
    var products = this.make$("prodotti");

    var args = req.args.body;

    console.log(args);

    products.nome = args.nome;
    products.categoria = args.categoria;
    products.descrizione = args.descrizione;
    products.prezzo = args.prezzo;

    products.save$(function(err, product) {
        done(err, products.data$(false));
    });
});



// get by Id  , PROBLEM!!!
seneca.add({
    area : "product",
    action : "fetch",
    criteria : "byId"
}, function(req, done) {
    console.log("HERE");

    var id = req.args.params.id;

    var product = this.make("prodotti");
    product.load$(id, done);
});

// LIST ALL
seneca.add({
    area : "product",
    action : "fetch"
}, function(args, done) {
    var products = this.make("prodotti");
    products.list$({}, done);
});


}

问题出在 getbyId 处理程序(路由 /products/fetch/byId/id_of_the 产品)。

到目前为止代码有效,但我想获得由 id_of_the product 表示的 id 变量不执行 var id = req.args.params.id; 但我希望它合并到 req 对象中,我想做 var id = req.id;

同样在 ADD 处理程序中,我做了 var args = req.args.body;但我希望看到 body 合并到 req 对象中,正如我在书中看到的那样 'Developing Microservices in node.js' 作者:大卫·冈萨雷斯。

当我想在处理程序中调用另一个传递产品 ID 的 seneca 操作时,问题就出现了。在这种情况下,id 可用于 req 对象,而不是在 url 参数中。 当然,我可以测试变量是否在 req.args.params 中定义,如果没有使用 req 对象中的变量,但这不是一个干净的解决方案。

当前版本的 seneca 和 seneca web 是否可行?我已经从 npm 安装了它们并且我有这些版本: 塞内卡 3.2.2 和塞内卡网络 2.0.0;

提前致谢!

您应该整理出您感兴趣的信息,验证其有效性并仅使用该信息调用基础操作。 role:web 可以成为在访问您的实际服务之前根据用户输入进行一般完整性检查的良好把关人。

请记住,用户可以传递任何内容作为路由参数、正文、查询字符串等。必须注意清理用户提供的信息,并且只将有效信息发送到基础操作中。

seneca.add('role:web,domain:product,action:getById', function (msg, done) {
  const id = parseInt(msg.args.params.id, 10)
  if (isNaN(id)) {
    return done(new Error('needs to be numeric'))
  }
  seneca.act('role:entity,domain:product,action:getById', {id}, done)
})

seneca.add('role:entity,domain:product,action:getById', function (msg, done) {
  this.make$('product').load$(msg.id, done)
})

从(上例中)检索有关请求的信息 msg.argsseneca-web 中的游戏名称 - 这是获取该信息的唯一方法。