无法读取属性的新错误处理,然后为未定义

时间:2020-09-16 09:20:01

标签: node.js

我正在尝试设置应用内购买验证(https://github.com/voltrue2/in-app-purchase),但此代码出现错误:

iap.setup()
  .then(() => {
    iap.validate(receipt).then(onSuccess).catch(onError);
  })

错误是“然后无法读取未定义的属性”和“然后每个都应返回值或抛出”

这是模板代码:

var iap = require('in-app-purchase');
iap.config({

    /* Configurations for HTTP request */
    requestDefaults: { /* Please refer to the request module documentation here: https://www.npmjs.com/package/request#requestoptions-callback */ },

    /* Configurations for Amazon Store */
    amazonAPIVersion: 2, // tells the module to use API version 2
    secret: 'abcdefghijklmnoporstuvwxyz', // this comes from Amazon
    // amazonValidationHost: http://localhost:8080/RVSSandbox, // Local sandbox URL for testing amazon sandbox receipts.

    /* Configurations for Apple */
    appleExcludeOldTransactions: true, // if you want to exclude old transaction, set this to true. Default is false
    applePassword: 'abcdefg...', // this comes from iTunes Connect (You need this to valiate subscriptions)

    /* Configurations for Google Service Account validation: You can validate with just packageName, productId, and purchaseToken */
    googleServiceAccount: {
        clientEmail: '<client email from Google API service account JSON key file>',
        privateKey: '<private key string from Google API service account JSON key file>'
    },

    /* Configurations for Google Play */
    googlePublicKeyPath: 'path/to/public/key/directory/', // this is the path to the directory containing iap-sanbox/iap-live files
    googlePublicKeyStrSandBox: 'publicKeySandboxString', // this is the google iap-sandbox public key string
    googlePublicKeyStrLive: 'publicKeyLiveString', // this is the google iap-live public key string
    googleAccToken: 'abcdef...', // optional, for Google Play subscriptions
    googleRefToken: 'dddd...', // optional, for Google Play subscritions
    googleClientID: 'aaaa', // optional, for Google Play subscriptions
    googleClientSecret: 'bbbb', // optional, for Google Play subscriptions

    /* Configurations for Roku */
    rokuApiKey: 'aaaa...', // this comes from Roku Developer Dashboard

    /* Configurations for Facebook (Payments Lite) */
    facebookAppId: '112233445566778',
    facebookAppSecret: 'cafebabedeadbeefabcdef0123456789',

    /* Configurations all platforms */
    test: true, // For Apple and Googl Play to force Sandbox validation only
    verbose: true // Output debug logs to stdout stream
});
iap.setup()
  .then(() => {
    iap.validate(receipt).then(onSuccess).catch(onError);
  })
  .catch((error) => {
    // error...
  });

function onSuccess(validatedData) {
    // validatedData: the actual content of the validated receipt
    // validatedData also contains the original receipt
    var options = {
        ignoreCanceled: true, // Apple ONLY (for now...): purchaseData will NOT contain cancceled items
        ignoreExpired: true // purchaseData will NOT contain exipired subscription items
    };
    // validatedData contains sandbox: true/false for Apple and Amazon
    var purchaseData = iap.getPurchaseData(validatedData, options);
}

function onError(error) {
    // failed to validate the receipt...
}

这是iapsetup:

module.exports.setup = function (cb) {
    if (!cb && Promise) {
        return new Promise(function (resolve, reject) {
            module.exports.setup(handlePromisedFunctionCb(resolve, reject));
        });
    }
    async.series([
        function (next) {
            apple.setup(next);
        },
        function (next) {
            google.setup(next);
        },
        function (next) {
            amazon.setup(next);
        }
    ], cb);
};

这是iap.validate:

module.exports.validate = function (service, receipt, cb) {
    if (receipt === undefined && cb === undefined) {
        // we are given 1 argument as: const promise = .validate(receipt)
        receipt = service;
        service = module.exports.getService(receipt);
    }
    if (cb === undefined && typeof receipt === 'function') {
        // we are given 2 arguments as: .validate(receipt, cb)
        cb = receipt;
        receipt = service;
        service = module.exports.getService(receipt);
    }
    if (!cb && Promise) {
        return new Promise(function (resolve, reject) {
            module.exports.validate(
                service,
                receipt,
                handlePromisedFunctionCb(resolve, reject)
            );
        });
    }

我已经阅读了许多有关此的SO文章,似乎我需要返回一个诺言,但是从它的外观看,已经返回了一个诺言,并且还返回了值。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

这里有很多噪音,但是乍一看,伸出的部分是:

    if (!cb && Promise) {
        return new Promise(function (resolve, reject) {
            module.exports.setup(handlePromisedFunctionCb(resolve, reject));
        });
    }

您实际上并没有解决/拒绝任何事情 也许像

module.exports.setup = function (cb) {
    if (!cb && Promise) {
        return new Promise(function (resolve, reject) {
            const exp = module.exports.setup(handlePromisedFunctionCb(resolve, reject));
        resolve(exp)
        });
    }