0

我正在从事一个大学项目,并且正在使用 babel 转译的 node js 编写后端,以便使用 async/await

我正在生成源映射以正确调试,但它做了一些非常糟糕的工作

例如 - 我不能在“if”或“return”语句上设置断点我担心这只是在安装了一些额外的 babel 插件之后才发生的,我在尝试解决一些错误时必须安装

这是我的 package.json:

"axios": "^0.15.3",
"babel-runtime": "^6.20.0",
"body-parser": "^1.15.2",
"chai": "^3.5.0",
"chai-datetime": "^1.4.1",
"ejs": "^2.5.2",
"exceljs": "^0.2.39",
"express": "^4.14.0",
"flat": "^2.0.1",
"generator": "^1.0.1",
"hash": "^0.2.0",
"hash-generator": "^0.1.0",
"mocha": "^3.2.0",
"moment": "^2.17.1",
"mongoose": "^4.7.2",
"nodemailer": "^2.7.0",
"react": "^15.4.1",
"react-bootstrap": "^0.30.7",
"react-bootstrap-table": "^2.9.0",
"react-dom": "^15.4.1",
"react-router": "^3.0.0",
"react-uniform": "^1.2.1",
"rimraf": "^2.5.4",
"underscore": "^1.8.3",
"winston": "^2.3.0",
"winston-mongodb": "^1.6.5"


"devDependencies": {
"babel-cli": "^6.18.0",
"babel-core": "^6.21.0",
"babel-loader": "^6.2.10",
"babel-plugin-transform-runtime": "^6.15.0",
"babel-polyfill": "^6.20.0",
"babel-preset-es2015": "^6.18.0",
"babel-preset-es2016": "^6.16.0",
"babel-preset-latest": "^6.16.0",
"babel-preset-react": "^6.16.0",
"babel-preset-stage-0": "^6.16.0",
"babel-watch": "^2.0.4",
"html-webpack-plugin": "^2.24.1",
"webpack": "^1.13.3",
"webpack-dev-server": "^1.16.2"

它有点削减,但你明白了

奇怪的是转译的 js 文件看起来很奇怪

var addEncouragement = function () {
var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(sessionId, encouragementDetails) {
    var user, encouragement, i, exist, products, res;
    return _regenerator2.default.wrap(function _callee$(_context) {
        while (1) {
            switch (_context.prev = _context.next) {
                case 0:
                    logger.info('Services.Encouragement.index.addEncouragement', { 'session-id': sessionId });
                    _context.next = 3;
                    return permissions.validatePermissionForSessionId(sessionId, 'addEncouragement');

                case 3:
                    user = _context.sent;

                    if (!(user != null)) {
                        _context.next = 29;
                        break;
                    }

                    encouragement = new encouragementModel();

                    encouragement.active = encouragementDetails.active;
                    encouragement.numOfProducts = encouragementDetails.numOfProducts;
                    encouragement.rate = encouragementDetails.rate;

                    //check if all the products id belongs to products
                    i = 0;

                case 10:
                    if (!(i < encouragementDetails.products.length)) {
                        _context.next = 19;
                        break;
                    }

                    _context.next = 13;
                    return dal.getProductById(encouragementDetails.products[i]);

                case 13:
                    exist = _context.sent;

                    if (!(exist == null)) {
                        _context.next = 16;
                        break;
                    }

                    return _context.abrupt('return', { 'encouragement': null, 'code': 404, 'err': 'product not found' });

                case 16:
                    i++;
                    _context.next = 10;
                    break;

                case 19:
                    _context.next = 21;
                    return dal.getProductsById(encouragementDetails);

                case 21:
                    products = _context.sent;

                    encouragement.products = products;

                    _context.next = 25;
                    return dal.addEncouragement(encouragement);

                case 25:
                    res = _context.sent;
                    return _context.abrupt('return', { 'encouragement': encouragement, 'code': 200, 'err': null });

                case 29:
                    return _context.abrupt('return', { 'encouragement': null, 'code': 401, 'err': 'permission denied' });

                case 30:
                case 'end':
                    return _context.stop();
            }
        }
    }, _callee, this);
}));

虽然这是源函数:

let addEncouragement = async function(sessionId, encouragementDetails) {
logger.info('Services.Encouragement.index.addEncouragement', {'session-id': sessionId});
let user = await permissions.validatePermissionForSessionId(sessionId, 'addEncouragement');
//check if to user have the permissions
if(user != null) {
    let encouragement = new encouragementModel();
    encouragement.active = encouragementDetails.active;
    encouragement.numOfProducts = encouragementDetails.numOfProducts;
    encouragement.rate = encouragementDetails.rate;

    //check if all the products id belongs to products
    for (let i = 0; i < encouragementDetails.products.length; i++) {
       let exist = await dal.getProductById(encouragementDetails.products[i]);
       if(exist == null){
           return {'encouragement': null, 'code': 404, 'err': 'product not found'};
       }
    }

    //create id object from the string id
    let products = await dal.getProductsById(encouragementDetails);
    encouragement.products = products;

    let res = await dal.addEncouragement(encouragement);
    return {'encouragement': encouragement, 'code': 200, 'err': null};
}
else{
    return {'encouragement': null, 'code': 401, 'err': 'permission denied'};
}

};

这是文件的开头:

"use strict";
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

我将不胜感激,或者如果有人可以告诉我这里出了什么问题以及为什么我无法正确调试

谢谢你

4

0 回答 0