最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

如何使用节点JS服务层

IT培训 admin 1浏览 0评论

如何使用节点JS服务层

我是新的节点JS。我有我的应用程序使用的快递和sequelize。

这是我的路由器功能。

router.post('/add-merchant', [
    check('name').not().isEmpty(),
    check('city').not().isEmpty(),
    check('state').not().isEmpty(),
    check('country').not().isEmpty(),
], (req, res, next) => {
    try {
        const errors = validationResult(req);

        if (!errors.isEmpty()) {
            return res.json({ errors: errors.array()});
        }

        var merchant = merchantService.addMerchant(req);
        return res.json(merchant)
    } catch (error) {
        return res.json({"status": "error", "message": error.message})
    }
});

我已经创建了文件名为merchantService.js

我加入的代码在merchantService.js插入数据,并试图像这样

var merchant = merchantService.addMerchant(req);

但我不能得到商户服务的任何数据。这里是我的商户服务代码

变种模型需要=( “../模型”);

var merchantService = {
    addMerchant: (req) => {
        models.merchants.create(req.body).then((merchant) => {
            return merchant.dataValues
        });
    }
}

module.exports = merchantService;

我无法找到问题。请帮助任何人来解决这个问题。

提前致谢

回答如下:

你正在管理一个同步方式异步任务,这是行不通的。

你应该改变你的要求处理程序是这样的:

router.post('/add-merchant', [
  check('name').not().isEmpty(),
  check('city').not().isEmpty(),
  check('state').not().isEmpty(),
  check('country').not().isEmpty(),
], (req, res, next) => {
  try {
    const errors = validationResult(req);

    if (!errors.isEmpty()) {
      return res.json({ errors: errors.array() });
    }

     merchantService.addMerchant(req).then((merchant)=>{
      res.json(merchant)
    })
  } catch (error) {
    return res.json({ "status": "error", "message": error.message })
  }
});

而解决您的商户服务类似(请参阅为了启动诺言链return值):

const merchantService = {
  addMerchant: (req) => {
    return models.merchants.create(req.body)
      .then((merchant) => {
        return merchant.dataValues
      });
  }
}

module.exports = merchantService;

如何使用节点JS服务层

我是新的节点JS。我有我的应用程序使用的快递和sequelize。

这是我的路由器功能。

router.post('/add-merchant', [
    check('name').not().isEmpty(),
    check('city').not().isEmpty(),
    check('state').not().isEmpty(),
    check('country').not().isEmpty(),
], (req, res, next) => {
    try {
        const errors = validationResult(req);

        if (!errors.isEmpty()) {
            return res.json({ errors: errors.array()});
        }

        var merchant = merchantService.addMerchant(req);
        return res.json(merchant)
    } catch (error) {
        return res.json({"status": "error", "message": error.message})
    }
});

我已经创建了文件名为merchantService.js

我加入的代码在merchantService.js插入数据,并试图像这样

var merchant = merchantService.addMerchant(req);

但我不能得到商户服务的任何数据。这里是我的商户服务代码

变种模型需要=( “../模型”);

var merchantService = {
    addMerchant: (req) => {
        models.merchants.create(req.body).then((merchant) => {
            return merchant.dataValues
        });
    }
}

module.exports = merchantService;

我无法找到问题。请帮助任何人来解决这个问题。

提前致谢

回答如下:

你正在管理一个同步方式异步任务,这是行不通的。

你应该改变你的要求处理程序是这样的:

router.post('/add-merchant', [
  check('name').not().isEmpty(),
  check('city').not().isEmpty(),
  check('state').not().isEmpty(),
  check('country').not().isEmpty(),
], (req, res, next) => {
  try {
    const errors = validationResult(req);

    if (!errors.isEmpty()) {
      return res.json({ errors: errors.array() });
    }

     merchantService.addMerchant(req).then((merchant)=>{
      res.json(merchant)
    })
  } catch (error) {
    return res.json({ "status": "error", "message": error.message })
  }
});

而解决您的商户服务类似(请参阅为了启动诺言链return值):

const merchantService = {
  addMerchant: (req) => {
    return models.merchants.create(req.body)
      .then((merchant) => {
        return merchant.dataValues
      });
  }
}

module.exports = merchantService;

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论