Javascript 未处理承诺例外
我是JavaScript和pubnub方面的新手。我正在尝试用javascript发布NUB。下面是我的代码Javascript 未处理承诺例外,javascript,node.js,exception-handling,promise,pubnub,Javascript,Node.js,Exception Handling,Promise,Pubnub,我是JavaScript和pubnub方面的新手。我正在尝试用javascript发布NUB。下面是我的代码 const PubNub = require('pubnub') const credentials = { publishKey : 'pub-c-58dae4c4-4a11-4ff5-ad98-a69bf5c086bc', subscribeKey : 'sub-c-5d1ca66a-2308-11e9-b712-2656c4b29a42', secre
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
有人能帮忙吗?我相信它是在对你大喊大叫,因为它抛出了一个错误,而你没有抓住它
this.pubnub
.publish( {channel, message })
.then(result => {
// you probably should handle this when successful?
})
.catch(error => { // catch the errors
console.log(error);
});
我认为你的问题与公共事务无关。PubNub SDK的
publish
使用回调模式。使用此选项可查看发布结果:
this.pubnub.publish( {channel, message }, function(status, response) {
console.log('Publish Result: ', status, response)
});
如果未看到记录的PubNub错误,则问题可能与NPM配置有关。如果是这种情况,请在此处尝试回答无法在本地获取颁发者证书的问题:如果您还没有定义
parsedMessage
const parsedMessage = JSON.parse(message);
this.publish.publish({channel,message});在您的更改之后,我得到了一些关于错误的简要描述,无法\u获取\u颁发者\u证书\u本地声音,就像您的服务器上有代理一样?现在有点不同了。