getting-started
getting-started copied to clipboard
how to hot-import config?
I'm trying to hot-import config:
const { hotImport } = require('hot-import')
async function onMessage (message) {
const cfg = await hotImport('./config')
console.log(`Received message: ${message} from ${cfg.db.host}`)
}
It works for the first received message. However on receiving the 2nd message, the program errored out with:
(node:80803) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'db' of undefined
at Wechaty.onMessage (/path/to/wx/hot-import-bot/on-message.js:25:56)
(node:80803) 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)
How can I make it works please?
Made another attempt to fix it myself, still nok
Hi, I did not use a constant in hotMod before, however, I had just checked the document and it seems should support it. (at https://github.com/zixia/hot-import )
Can you try to run the example at https://github.com/zixia/hot-import#full-example ? the source is at https://github.com/zixia/hot-import/blob/master/examples/demo-js.ts
I'll also have a look into your code(which use a constant in module like cfg.db.host) later.
I think the problem is not constant in hotMod, but the **await** hotImport in **async** function onMessage that is causing the problem.
Once again, It works for the first received message but not the second.
Did you find any way to resolve this issue and could you share it for us? Thanks.
I think ... the await hotImport in async function onMessage that is causing the problem.
Moving the await hotImport out of the async onMessage event-handling function solved the problem, because the redefinition was indeed the problem.
Yes, we should only use hotImport once for a module from the beginning of the code.
Glad to hear from you that this issue had been fixed. Cheers and happy Christmas!
Oh, actually it is not working -- It seems working because I have a let cfg = require('./config') at the top of the script, but I was not able to make it hotImported.
Reopening it, and hope someone can put hot-import config into the demo of https://github.com/wechaty/wechaty-getting-started/tree/master/examples/professional/hot-import-bot
It isn't hard for me if I know nodejs well.
thx
Really? I will have a look into it when I have time.
On the other hand, I remembered that the hotImport feature is working inside Docker container too.
The same problem happens to me too, if it's imported twice there's a problem. The error repros on docker
I changed to this
if(!config)
{
config = await hotImport('./config.js')
}