wechaty / puppet-supports

Wechaty Puppet Services (WPS)
https://wechaty.js.org/docs/puppet-services/
Apache License 2.0
33 stars 4 forks source link

[BUG]WorkPro:bot.Contact.find调用报错 #359

Closed ccgulan closed 9 months ago

ccgulan commented 9 months ago

出现如下错误: init error: Error: Wechaty User Module (WUM, for example: wechaty.Room) can not be used before wechaty.start()! at guardWechatify (file:///E:/wechat-robot-worpro_2/node_modules/wechaty/dist/esm/src/wechaty-mixins/wechatify-user-module-mixin.js:78:11) at get Contact [as Contact] (file:///E:/wechat-robot-worpro_2/node_modules/wechaty/dist/esm/src/wechaty-mixins/wechatify-user-module-mixin.js:23:32) at file:///E:/wechat-robot-worpro_2/bot.js:67:28

代码如下: try { bot.start() .then(() => log.info('StarterBot', 'Starter Bot Started.')) .catch(e => log.error('StarterBot', e)) const contact = await bot.Contact.find({name: 'xucj'}) await contact.say('wechaty start!')

} catch (error) { console.log('init error: ', error); }

hcfw007 commented 9 months ago

因为你的find没有写在then里面,start也没有await,所以等于还没有start就find了。

ccgulan commented 9 months ago

@hcfw007 我延迟了发送,但是还是出错 代码如下: async function sendFind(){ var date = new Date(); var date_str= ${date.getFullYear()}-${date.getMonth()}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()};

const contact = await bot.Contact.find({ name: 'xucj' }) ;
await contact.say(date_str+'  hello,I am fine!')  

}

export function callInit() {

setInterval(call, 2000) setInterval(sendFind, 60000)

}

错误如下: Unhandled Rejection at: Promise {

TypeError: Cannot read properties of undefined (reading 'Contact') at Timeout.sendFind [as _onTimeout] (file:///E:/wechat-robot/on-message.js:1081:31) at listOnTimeout (node:internal/timers:569:17) at process.processTimers (node:internal/timers:512:7) } reason: TypeError: Cannot read properties of undefined (reading 'Contact') at Timeout.sendFind [as _onTimeout] (file:///E:/wechat-robot/on-message.js:1081:31) at listOnTimeout (node:internal/timers:569:17) at process.processTimers (node:internal/timers:512:7)
ccgulan commented 9 months ago

@su-chang @hcfw007 对话框打开过的联系人,能主动发送信息,如果对话框没有打开过的,调用如下代码就会出错,请问是什么缘故 const contact = await bot.Contact.find({ name: 'xucj' }) ; await contact.say(date_str+' hello,I am fine!')

hcfw007 commented 9 months ago

从抛错看,是bot没定义(是undefined),可能是你闭包没处理好,但具体还是要看完整代码才知道。

hcfw007 commented 9 months ago

无后续回复,issue关闭。