parseInt () в скрипте, возвращающем NaN, потому что синтаксис jsonpath, похоже, не читается правильно - PullRequest
0 голосов
/ 04 мая 2018

Используя обозначение точки jsonpath, ключ, который мне нужен, это-

ProductDetails.Prices.BillingDivisor

Пример документа JSON, показанного ниже ...

{
    "UniqueID": "50962",
    "InvNum": "17001107",
    "Location": "017",
    "InvoiceDate": "6/19/2017 12:00:00 AM",
    "LINEITEM": "1",
    "CUSTID": "011022",
    "DEPTID": "004305",
    "PRODID": "070023",
    "QUANT": "0",
    "Unitprice": 1.887,
    "BILLUNIT": "GL",
    "QuantShare": 2000,
    "Tax": "False",
    "TaxExempt": "",
    "PriceType": "List",
    "DumItem": "False",
    "SourceNumber": "0",
    "SourceLocation": "",
    "SourceDate": "1/1/1900 12:00:00 AM",
    "LastUpdated": "2018-05-03T18:49:00-0400",
    "BookGuid": null,
    "ProductDetails": {
        "UniqueKey": 98269,
        "DepartmentID": "004305",
        "ProductID": "070023",
        "Accounts": {
            "DepartmentID": "004305",
            "ProductID": "070023",
            "SalesTaxable": false,
            "SalesAccount": "",
            "BeginningYearBalance": 0,
            "EndOfMonth1": 0,
            "EndOfMonth2": 0,
            "EndOfMonth3": 0,
            "EndOfMonth4": 0,
            "EndOfMonth5": 0,
            "EndOfMonth6": 0,
            "EndOfMonth7": 0,
            "EndOfMonth8": 0,
            "EndOfMonth9": 0,
            "EndOfMonth10": 0,
            "EndOfMonth11": 0,
            "EndOfMonth12": 0,
            "ProductStateTaxable": false,
            "PurchaseAccount": "",
            "FreightAccount": "",
            "PurchaseDiscount": ""
        },
        "Prices": {
            "DepartmentID": "004305",
            "ProductID": "070023",
            "BillingUnits": "GL",
            "BillingDivisor": "1",
            "AverageCost": "2.129",
            "List": "0",
            "LastPurchaseCost": "2.129"
        },
        "Classifications": {
            "Name-0": "5314: Dieselex",
            "Name-1": "B11 DYED:  B11 DYED",
            "Name-2": "Gallon Products",
            "Name-3": "FUEL-CvilleDYED",
            "Name-4": "Energy Comm"
        },
        "Attributes": {
            "Value-0": "",
            "Name-0": "Bag Weight",
            "Required-0": false,
            "MaximumLength-0": 0,
            "DisallowDuplicates-0": false,
            "Value-1": "",
            "Name-1": "GTIN",
            "Required-1": false,
            "MaximumLength-1": 0,
            "DisallowDuplicates-1": false
        },
        "LastUpdated": "2018-05-03T18:19:18-0400",
        "ProductClassification": "5314: Dieselex"
    }
}

Используя следующий javascript, все значения возвращаются правильно, кроме этой части:

ParseInt (data.ProductDetails.Prices.BillingDivisor)

так как я продолжаю получать результат в виде NaN вместо 1.

var bulk = db.bookingsMaster.initializeOrderedBulkOp();
var counter = 0;
db.bookingsMaster.find().forEach(function (data) {
    var updoc = {
        "$set": {
        }
    };
    var unitPrice = "Unitprice";
    updoc[ "$set"][unitPrice] = parseFloat(data.Unitprice);

    var quantShare = "QuantShare";
    updoc[ "$set"][quantShare] = parseFloat(data.QuantShare);

    var billingDivisor = "ProductDetails.Prices.BillingDivisor";
    updoc[ "$set"][billingDivisor] = parseInt(data.ProductDetails.Prices.BillingDivisor);

    // queue the update
    bulk.find({
        "_id": data._id
    }).update(updoc);
    counter++;
    // Drain and re-initialize every 1000 update statements
    if (counter % 1000 == 0) {
        bulk.execute();
        bulk = db.bookingsMaster.initializeOrderedBulkOp();
    }
})
// Add the rest in the queue
if (counter % 1000 != 0) bulk.execute();

Любая помощь очень ценится.

1 Ответ

0 голосов
/ 04 мая 2018

Ваша проблема в том, что, в отличие от Unitprice и QuantShare, BillingDivisor на самом деле представляет собой строку с двойными кавычками вокруг 1. Если вы попробуете

console.log(parseInt('"1"'))

вы получите NaN, как видите.

Вам нужно убрать двойные кавычки со значения, например,

console.log(parseInt('"1"'.replace('"','')))

, который возвращает 1, по желанию. Итак, в вашем коде вам нужно

parseInt(data.ProductDetails.Prices.BillingDivisor.replace('"',''))
...