-1

Following is a part of the nodejs script.

const util = require('util');
const exec = util.promisify(require('child_process').exec);


fs.writeFile( filepath, dom.window.document.querySelectorAll("note")[i].textContent, function(err) {
            if(err) { return console.log(err); }
});

const text_content = dom.window.document.querySelectorAll("note")[i].textContent ; 

async function exec_python3() {
  const { stdout, stderr } = await exec(`bash -c 'python3 ${filepath}'`);
  text_content = stdout ; 
}

exec_python3() ; 

dom.window.document.querySelectorAll("note")[i].textContent is a jsdom object.

What I want to do is replace content in dom.window.document.querySelectorAll("note")[i].textContent with stdout of executed Python3. For example, currently value of dom.window.document.querySelectorAll("note")[i].textContent is print("foo") and the value will be foo after exec_python3() ;.

But above script occurs error Assignment to constant variable.. I have no idea what I have to do, any idea? thanks.

k23j4
  • 723
  • 2
  • 8
  • 15
  • You should probably start by reading up on what `const` means – Quentin Aug 15 '19 at 07:57
  • Which part of `error: Assignment to constant variable` do you not understand? You cannot assign a value to a variable declared with `const` after the declaration. Use `let` when you declare the variable if you want to be able to assign to it later. I tend to think this isn't your real issue, but the point of the question is hard to follow so I commented on the error you report. – jfriend00 Aug 15 '19 at 07:59
  • I knew that. but If you use `let` to define the statement you'll get `TypeError: Cannot set property 'textContent' of undefined` error. That made me confuse. – k23j4 Aug 15 '19 at 08:27

1 Answers1

0

If you want to change dom.window.document.querySelectorAll("note")[i].textContent then you need to change that.

Copying its value to a variable and then changing the value of that variable isn't going to touch dom.window.document.querySelectorAll("note")[i].textContent.

So:

dom.window.document.querySelectorAll("note")[i].textContent = stdout;

As for your error, while fixing it won't fix your problem, the entire point of using const is that you set it once and then can't change it. Use let if you want a variable you can change.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • If you just type the above one within `async function exec_python3()`, you'll get ` TypeError: Cannot set property 'textContent' of undefined` error. That's why I have added a const line. – k23j4 Aug 15 '19 at 08:25
  • @k23j4 — there's no enough information in the question to determine why it would be defined when you read from it but not when you want to write to it. Presumably, `i` has changed. And presumably, the solution is https://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example – Quentin Aug 15 '19 at 08:36