Исключение необработанного обещания - PullRequest
0 голосов
/ 30 января 2019

Я новичок в JavaScript и pubnub.Я пытаюсь опубликовать в Javascript.Ниже приведен мой код

const PubNub = require('pubnub')

const credentials = {
    publishKey   : 'pub-c-58dae4c4-4a11-4ff5-ad98-a69bf5c086bc',
    subscribeKey : 'sub-c-5d1ca66a-2308-11e9-b712-2656c4b29a42',
    secretKey    : 'sub-c-5d1ca66a-2308-11e9-b712-2656c4b29a42'
}

const CHANNEL = {
    TEST : 'TEST',
    TESTTWO : 'TESTTWO',
    BLOCKCHAIN : 'BLOCKCHAIN'
}

class PubSub {
    constructor(blockchain) {
        this.blockchain = blockchain;
        this.pubnub = new PubNub(credentials);
        this.pubnub.subscribe( { channel: [Object.values(CHANNEL)] });
        this.pubnub.addListener( this.listener())
    }

    listener() {    
        return {
            message: messageObject => {
                const { channel, message } = messageObject
                console.log("Message received on channel "+channel + ". Message is "+message);
                if (channel === CHANNEL.BLOCKCHAIN) {
                    this.blockchain.replaceChain(parsedMessage);
                }
            }
        }
publish( {channel, message}) {

        this.pubnub.publish( {channel, message });
    }

broadcastChain() {
        this.publish( { 
            channel: CHANNEL.BLOCKCHAIN,
            message: JSON.stringify(this.blockchain.chain)
            })
        }
}
module.exports = PubSub

Я получаю исключение необработанного обещания.Ниже приведены журналы ошибок:

(node:9456) UnhandledPromiseRejectionWarning: Error: Validation failed, check status for details
    at new PubNubError (C:\Users\sagardeshpande.deshp\test_cryptochain\node_modules\pubnub\lib\core\components\endpoint.js:134:112)
    at _class.exports.default (C:\Users\sagardeshpande.deshp\test_cryptochain\node_modules\pubnub\lib\core\components\endpoint.js:32:31)
    at PubSub.publish (C:\Users\sagardeshpande.deshp\test_cryptochain\pubnub.js:58:25)
    at PubSub.broadcastChain (C:\Users\sagardeshpande.deshp\test_cryptochain\pubnub.js:66:14)
    at Timeout.setTimeout [as _onTimeout] (C:\Users\sagardeshpande.deshp\test_cryptochain\index.js:10:26)
    at ontimeout (timers.js:436:11)
    at tryOnTimeout (timers.js:300:5)
    at listOnTimeout (timers.js:263:5)
    at Timer.processTimers (timers.js:223:10)
(node:9456) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:9456) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code

Может кто-нибудь помочь, пожалуйста?

Ответы [ 3 ]

0 голосов
/ 31 января 2019

Не думаю, что ваша проблема связана с PubNub.publish PubNub SDK использует шаблон обратного вызова.Используйте это для просмотра результатов публикации:

this.pubnub.publish( {channel, message }, function(status, response) {
    console.log('Publish Result: ', status, response)
});

Если вы не видите зарегистрированную ошибку PubNub, возможно, проблема связана с вашей конфигурацией NPM.Если это так, попробуйте этот ответ на UNABLE_TO_GET_ISSUER_CERT_LOCALLY здесь: https://stackoverflow.com/a/45884819/6193736

0 голосов
/ 28 марта 2019

Вы должны попытаться определить parsedMessage, если вы еще этого не сделали.

const parsedMessage = JSON.parse(message);
0 голосов
/ 30 января 2019

Я полагаю, что он кричит на вас, потому что он выдал ошибку, а вы ее не поймали.

this.pubnub
  .publish( {channel, message })
    .then(result => {
      // you probably should handle this when successful?
    })
    .catch(error => {  // catch the errors
      console.log(error);
    });
...