不能 trim exceljs 单元格值

Cannot trim exceljs cell value

我想从 Excel 文件中获取值:

var Excel = require("exceljs");
var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, path.join(__dirname, '../config/uploads/'));
    },
    filename: function (req, file, cb) {
        cb(null, file.originalname);
    }
});
var upload = multer({ storage: storage });
router.post("/affecterConducteur", upload.single('fichier'), function(req, res) {
    var filename = req.file.destination + req.file.filename;
    var workbook = new Excel.Workbook();
    workbook.xlsx.readFile(filename).then(function() {
        var sheet = workbook.getWorksheet(1);
        for(var r=2; r<=sheet.rowCount; r++) {
            var row = sheet.getRow(r);
            var msisdn = row.getCell(1).value, immatriculation = row.getCell(2).value;
            if (msisdn != null) {
                msisdn = msisdn.trim();
                msisdn = (msisdn.substr(0,3) == "034" ? msisdn : "0".concat(msisdn));
                immatriculation = immatriculation.trim();
                immatriculation = immatriculation.replace(/ /g, "");
                var sql = "insert into "+db.getPrefixNomTables()+"conducteur(type_conducteur_id, msisdn) "+
                          "values((select type_conducteur_id from "+db.getPrefixNomTables()+"type_conducteur where lower(type_conducteur_lib) = 'cdz'), '"+msisdn+"')";
                connexion.query(sql, function(err, rows) {
                    if (err)
                        throw err;
                    var maj = "update "+db.getPrefixNomTables()+"vehicule "+
                              "set conducteur_id = "+rows.insertId+" where replace(immatriculation, ' ', '') = '"+immatriculation+"'";
                    connexion.query(maj, function(err2, rows2) {
                        if (err2)
                            throw err2;
                    });
                });
            }
        }
        res.send("");
    });
});

在运行时我得到 TypeError: msisdn.trim is not a function。那有什么问题吗?

您可能想要检查 msisdn 是否为字符串,而不是检查它是否不为空。

if(typeof msisdn === 'string')

它抱怨说 trim 不是一个函数,因为你正试图在不是字符串的东西上执行它。