# How to get "text" from "speech to text" that needed using in function node

**URL:** <https://discourse.nodered.org/t/how-to-get-text-from-speech-to-text-that-needed-using-in-function-node/24954>\
**Category:** General\
**Created:** [16 April 2020 15:25 UTC](https://discourse.nodered.org/t/how-to-get-text-from-speech-to-text-that-needed-using-in-function-node/24954 "2020-04-16T15:25:24Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![lingzhongli](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lingzhongli/32/20078_2.png) [@lingzhongli](https://discourse.nodered.org/u/lingzhongli)\
**Post date:** [16 April 2020 15:25 UTC](https://discourse.nodered.org/t/how-to-get-text-from-speech-to-text-that-needed-using-in-function-node/24954/1 "2020-04-16T15:25:25Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/2/92c778d4fc9f6de63ee074314fdf4164dc250cac.png)

```auto
var topi =msg.topic;
msg.topic = "SELECT VOICE FROM USER";//data voice from database

var transcription =msg.transcription;//data from microphone (input data)

if(topic==transcription){
    
    //upload name to history database from user database
    msg.topic = INSERT INTO HISTORY (NAME) VALUES (SELECT NAME FROM USER WHERE VOICE = transcription);
    
    //audio sound
    msg.payload="Welcome";
    
    
}

if(topic!=transcription){
    
    //audio sound
    msg.payload="Access Denied";
    
}

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/b/0b0a1939ed4ae3644aab97f0258f66f3c085d58b.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/3/3350a071305f44c14a06969731b7e59163a18289.png)

I want compare the text (generate from `speech to text`) that i get from microphone with the data in database.  
So, how to write in `function node` to get that text?  
is that using variable `transcription` ?(I think my code is wrong)

And i want upload the NAME 's data to HISTORY database from USER database if the input text from microphone is same in USER database.  
How to write the correct it?

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [16 April 2020 15:32 UTC](https://discourse.nodered.org/t/how-to-get-text-from-speech-to-text-that-needed-using-in-function-node/24954/2 "2020-04-16T15:32:43Z")

</div>

You have a typo. you define your var your comparing as `var topi = msg.topic` instead of `var topic = msg.topic` so when you try to reference `topic` in your if statement its not declared.

Johannes

---

<div class="post-metadata">

**Author:** ![lingzhongli](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lingzhongli/32/20078_2.png) [@lingzhongli](https://discourse.nodered.org/u/lingzhongli)\
**Post date:** [16 April 2020 15:34 UTC](https://discourse.nodered.org/t/how-to-get-text-from-speech-to-text-that-needed-using-in-function-node/24954/3 "2020-04-16T15:34:01Z")

</div>

okey, but still have error

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [16 April 2020 15:35 UTC](https://discourse.nodered.org/t/how-to-get-text-from-speech-to-text-that-needed-using-in-function-node/24954/4 "2020-04-16T15:35:13Z")

</div>

What does the debug say? Can you show as a debug of the complete msg object the speech to text node puts out?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [16 April 2020 15:35 UTC](https://discourse.nodered.org/t/how-to-get-text-from-speech-to-text-that-needed-using-in-function-node/24954/5 "2020-04-16T15:35:27Z")

</div>

> [@lingzhongli](#):
>
> msg.topic = INSERT INTO HISTORY (NAME) VALUES (SELECT NAME FROM USER WHERE VOICE = transcription);

is not a string.... you're going to need some quotes " in there...

---

<div class="post-metadata">

**Author:** ![lingzhongli](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lingzhongli/32/20078_2.png) [@lingzhongli](https://discourse.nodered.org/u/lingzhongli)\
**Post date:** [16 April 2020 15:36 UTC](https://discourse.nodered.org/t/how-to-get-text-from-speech-to-text-that-needed-using-in-function-node/24954/6 "2020-04-16T15:36:28Z")

</div>

```auto
[{"id":"ee60c3e3.69e03","type":"mysql","z":"98f4cc17.965b","mydb":"7c738c44.e9a364","name":"DB_MYSQL","x":670,"y":260,"wires":[[]]},{"id":"5d766314.29f91c","type":"function","z":"98f4cc17.965b","name":"Verify Speech","func":"var topic =msg.topic;\nmsg.topic = \"SELECT VOICE FROM USER\";//data voice from database\n\nvar transcription =msg.transcription;//data from microphone (input data)\n\n\nif(topic==transcription){\n \n //upload name to history database from user database\n msg.topic = INSERT INTO HISTORY (NAME) VALUES (SELECT NAME FROM USER WHERE VOICE = transcription);\n \n //audio sound\n msg.payload=\"Welcome\";\n \n \n}\n\nif(topic!=transcription){\n \n //audio sound\n msg.payload=\"Access Denied\";\n \n}","outputs":2,"noerr":17,"x":420,"y":320,"wires":[["ee60c3e3.69e03","5c178158.4733f"],["91d9fd2a.af375"]]},{"id":"2b9d8dd1.390142","type":"watson-speech-to-text","z":"98f4cc17.965b","name":"","alternatives":1,"speakerlabels":true,"smartformatting":false,"lang":"en-US","langhidden":"en-US","langcustom":"NoCustomisationSetting","langcustomhidden":"","custom-weight":"0.5","band":"NarrowbandModel","bandhidden":"NarrowbandModel","keywords":"","keywords-threshold":"0.5","word-confidence":false,"password":"","apikey":"aEfplzvlM_B8MA01nsDy3fj6b3CnG6dOQEGISr2gHxxk","payload-response":true,"streaming-mode":false,"streaming-mute":false,"auto-connect":false,"discard-listening":false,"disable-precheck":false,"service-endpoint":"https://api.us-south.speech-to-text.watson.cloud.ibm.com/instances/4ec7115d-1cbf-4ac7-b141-9c9a675fbdac","x":160,"y":340,"wires":[["338d77fb.3e9f88","5d766314.29f91c"]]},{"id":"9c343afa.80e438","type":"microphone","z":"98f4cc17.965b","name":"","x":110,"y":240,"wires":[["2b9d8dd1.390142"]]},{"id":"338d77fb.3e9f88","type":"debug","z":"98f4cc17.965b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"transcription","targetType":"msg","x":410,"y":380,"wires":[]},{"id":"595861ce.b689c","type":"play audio","z":"98f4cc17.965b","name":"Access Denied","voice":"0","x":880,"y":400,"wires":[]},{"id":"d44f4fcb.4f531","type":"play audio","z":"98f4cc17.965b","name":"Welcome","voice":"0","x":880,"y":320,"wires":[]},{"id":"67ca3d11.6eb434","type":"comment","z":"98f4cc17.965b","name":"","info":"Verify the speech from microphone are same with “VOICE” in User table or not.\n\nIf same, then upload that particular user's name to HISTORY table and with the output sound \"Welcome\".\n\nIf not same just output sound \"Access denied\".","x":400,"y":280,"wires":[]},{"id":"4944a410.7d505c","type":"comment","z":"98f4cc17.965b","name":"","info":"To see the speech from microphone to let us can check the speech is from microphone is what we want speech.","x":420,"y":420,"wires":[]},{"id":"91d9fd2a.af375","type":"watson-text-to-speech","z":"98f4cc17.965b","name":"","lang":"en-GB","langhidden":"en-GB","langcustom":"NoCustomisationSetting","langcustomhidden":"","voice":"en-GB_KateV3Voice","voicehidden":"","format":"audio/wav","password":"","apikey":"kxIPJUsW_2zjWfDnhr8yNS4oPK5BPM4ywQGbZcQL9ZLl","payload-response":false,"service-endpoint":"https://api.us-south.text-to-speech.watson.cloud.ibm.com/instances/2ead23d4-2528-416d-8e53-c40a658d6bfd","x":680,"y":400,"wires":[["595861ce.b689c"]]},{"id":"5c178158.4733f","type":"watson-text-to-speech","z":"98f4cc17.965b","name":"","lang":"en-GB","langhidden":"en-GB","langcustom":"NoCustomisationSetting","langcustomhidden":"","voice":"en-GB_KateV3Voice","voicehidden":"","format":"audio/wav","password":"","apikey":"kxIPJUsW_2zjWfDnhr8yNS4oPK5BPM4ywQGbZcQL9ZLl","payload-response":false,"service-endpoint":"https://api.us-south.text-to-speech.watson.cloud.ibm.com/instances/2ead23d4-2528-416d-8e53-c40a658d6bfd","x":680,"y":320,"wires":[["d44f4fcb.4f531"]]},{"id":"7c738c44.e9a364","type":"MySQLdatabase","z":"","host":"165.22.51.44","port":"3306","db":"hireelec_vaes","tz":""}]

```

this is the flow ...

---

<div class="post-metadata">

**Author:** ![lingzhongli](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lingzhongli/32/20078_2.png) [@lingzhongli](https://discourse.nodered.org/u/lingzhongli)\
**Post date:** [16 April 2020 15:38 UTC](https://discourse.nodered.org/t/how-to-get-text-from-speech-to-text-that-needed-using-in-function-node/24954/7 "2020-04-16T15:38:37Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/a/4a66c5534d3bef8807313948b8a005b996b62485.png)

---

<div class="post-metadata">

**Author:** ![lingzhongli](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lingzhongli/32/20078_2.png) [@lingzhongli](https://discourse.nodered.org/u/lingzhongli)\
**Post date:** [16 April 2020 15:43 UTC](https://discourse.nodered.org/t/how-to-get-text-from-speech-to-text-that-needed-using-in-function-node/24954/8 "2020-04-16T15:43:42Z")

</div>

@dceejay

where need put quotes?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/0/e025a2ea06c8f4d189c5e6f3dda37fa20ea74810.png)

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [16 April 2020 15:54 UTC](https://discourse.nodered.org/t/how-to-get-text-from-speech-to-text-that-needed-using-in-function-node/24954/9 "2020-04-16T15:54:32Z")

</div>

What is this line supposed to be/do?  
You can only assign certain things to an object key like msg.topic.  
It has to be something like string, a number, a boolean or a javascript function.  
This is neither.  
To make it a string put everything after the = into quotes.  
And as far as I see this message your handling doesn’t have a topic.

---

<div class="post-metadata">

**Author:** ![lingzhongli](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lingzhongli/32/20078_2.png) [@lingzhongli](https://discourse.nodered.org/u/lingzhongli)\
**Post date:** [16 April 2020 18:09 UTC](https://discourse.nodered.org/t/how-to-get-text-from-speech-to-text-that-needed-using-in-function-node/24954/10 "2020-04-16T18:09:30Z")

</div>

upload the data.... and the data is user name who's voice text is same from input from microphone only

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [16 April 2020 18:34 UTC](https://discourse.nodered.org/t/how-to-get-text-from-speech-to-text-that-needed-using-in-function-node/24954/11 "2020-04-16T18:34:18Z")

</div>

You are trying to build a payload to write something to a database.  
In line 2 The function node has no way to access the data from your database where you saved the user names. So you have to get this data into nodered and save it in a global or flow var to be able to access it in a function node.  
Than you don’t just need the topic string but also a msg.payload.  
I would recommend for a start that you look into the documentation of nodered and read the chapters about the function node, context data and working with messages.  
[https://nodered.org/docs/user-guide/](https://nodered.org/docs/user-guide/)  
you will probably need a to change your function node to send the database and text to speech messages on 2 different exits as two distinct message objects.  
Johannes

Edit unfortunately I don’t know much about how to work with the mysql node.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [16 April 2020 19:25 UTC](https://discourse.nodered.org/t/how-to-get-text-from-speech-to-text-that-needed-using-in-function-node/24954/12 "2020-04-16T19:25:44Z")

</div>

`SELECT VOICE FROM USER;` will return the column 'VOICE' from `all` rows of the 'USER' table.

is that wat you want to do?

---

<div class="post-metadata">

**Author:** ![lingzhongli](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lingzhongli/32/20078_2.png) [@lingzhongli](https://discourse.nodered.org/u/lingzhongli)\
**Post date:** [16 April 2020 19:54 UTC](https://discourse.nodered.org/t/how-to-get-text-from-speech-to-text-that-needed-using-in-function-node/24954/13 "2020-04-16T19:54:16Z")

</div>

Ya...because that VOICE like the password....so, I need recieve the speech from microphone and compare one by one the VOICE in database , if same (mean password correct) , then just upload that particular user's name to HISTORY database.  
But now problem is how to write the code in verify speech ( function node) ? How to create or write a variable represent the text that recieved from microphone that genetated by speech to text node?

---

<div class="post-metadata">

**Author:** ![lingzhongli](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lingzhongli/32/20078_2.png) [@lingzhongli](https://discourse.nodered.org/u/lingzhongli)\
**Post date:** [17 April 2020 12:50 UTC](https://discourse.nodered.org/t/how-to-get-text-from-speech-to-text-that-needed-using-in-function-node/24954/14 "2020-04-17T12:50:55Z")

</div>

Ya...because that VOICE like the password....so, I need recieve the speech from microphone and compare one by one the VOICE in database , if same (mean password correct) , then just upload that particular user's name to HISTORY database.  
But now problem is how to write the code in verify speech ( function node) ? How to create or write a variable represent the text that recieved from microphone that genetated by speech to text node?

have any idea to write the code? or have any example?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [17 April 2020 14:25 UTC](https://discourse.nodered.org/t/how-to-get-text-from-speech-to-text-that-needed-using-in-function-node/24954/15 "2020-04-17T14:25:09Z")

</div>

Off hand my answer is NO. You store VOICE in the database - what type of value is it" when you analize it does it become a number that will match an equilivant number for the new sound that just came in?

If so, then you should be able to do a `SELECT VOICE FROM USER where VOICE = what-ever-you-call-the-new-sample;`

and it will return the single value or give you an indication that nothing was returned.

---

<div class="post-metadata">

**Author:** ![lingzhongli](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lingzhongli/32/20078_2.png) [@lingzhongli](https://discourse.nodered.org/u/lingzhongli)\
**Post date:** [17 April 2020 17:08 UTC](https://discourse.nodered.org/t/how-to-get-text-from-speech-to-text-that-needed-using-in-function-node/24954/16 "2020-04-17T17:08:18Z")

</div>

VOICE in database is in String and new sound is also String when it pass to speech to text node and become text....

so, i need declare one variable that can represent the text from speech to text and compare VOICE in database in String form.

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [17 April 2020 17:22 UTC](https://discourse.nodered.org/t/how-to-get-text-from-speech-to-text-that-needed-using-in-function-node/24954/17 "2020-04-17T17:22:02Z")

</div>

If I understand you right you will need to make a database query to get the content of Users/Voice before this speech to text flow and save it to a flow/global variable so that you can use it in this function as you otherwise wont be able to access it to check if the speech to text result is in it.  
So best read about flow and global context and how to work with it.  
[https://nodered.org/docs/user-guide/context](https://nodered.org/docs/user-guide/context)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [17 April 2020 17:40 UTC](https://discourse.nodered.org/t/how-to-get-text-from-speech-to-text-that-needed-using-in-function-node/24954/18 "2020-04-17T17:40:40Z")

</div>

So put the new sound string in msg.payload and send it to a `template` node to build your sql query like

```auto
select voice from user where user = '{{msg.payload}}'

```

The result needs to be in msg.topic and before connecting it to the `mysql` node, attach it to a debug node so you see that the query is correct. And it might be {{payload}} I can never remember.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [16 June 2020 17:40 UTC](https://discourse.nodered.org/t/how-to-get-text-from-speech-to-text-that-needed-using-in-function-node/24954/19 "2020-06-16T17:40:59Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
