iT邦幫忙

1

在heroku伺服器的nodejs中callbackfunction is not a function

  • 分享至 

  • twitterImage

我正在將語音轉成文字的結果回傳到index.js
然而在speechapisample.js裡面原本要callbackfunction(asrResult)的這行,卻說沒有這個function
如果我改成

if(callbackfunction) callbackfunction(asrResult);

則不會回傳結果。有查到要定義callbackfunction才能使用,卻不知道該從何定義起…

想請問各位高手大致上要如何處理?謝謝。

以下是目前的程式碼:

index.js

ffmpeg.on('close', (code) => {
      console.log("開始進行語音辨識..." + sender_psid + '.wav');
      speechApi.sendAudioFile( // <--開始
        'asr',
        'nli',
        true,
        '/tmp/' + sender_psid + '.wav',
        0, function (sttText) {
          console.log('辨識結果為:' + sttText);
          var sendMessage = '辨識結果為:' + sttText;
          response = {
            "text": `${sendMessage}`
          }
          callSendAPI(sender_psid, response);
          // sendTextMessage(sender_psid, '辨識結果為:'+ sttText);
          handleMessage(sender_psid, response);
        }
      );
    });

SpeechApiSample.js

SpeechApiSample.prototype.getRecognitionResult = function (apiName, seqValue, callbackFunction) {
	// var callbackFunction = '';
	var _this = this;
	var url = this.getBaseQueryUrl(apiName, seqValue);
	url += '&stop=1';
	// Request speech recognition service by HTTP GET
	request.get({
		url: url,
		headers: {
			'Cookie': this.cookies
		}
	}, function(err, res, body) {
		if (err) {
			console.log(err);
		}
	}).on('response', function(response) {
		var bufferhelper = new BufferHelper();
		response.on('data', function(chunk) {
			bufferhelper.concat(chunk);
		});

		response.on('end', function() {
			var body = iconv.decode(bufferhelper.toBuffer(), 'UTF-8');
			var result = JSON.parse(body);
			var return_status = result['data']['asr']['final'];
			// Try to get recognition result if uploaded successfully.
			// We just check the state by a lazy way :P , you should do it by JSON.
			if (return_status !== true) {
				console.log("\n----- Get Recognition Result -----\n");
				// Well, check by lazy way...again :P , do it by JSON please.
				delayed.delay(function () {
					_this.getRecognitionResult('asr', 'nli,seg', function(asrResult) {
						callbackFunction(asrResult);
					});
				}, 500);
			} else {
				console.log("\n----- Get Recognition Result -----\n");
				console.log("Result:\n\n" + body);

				// 語音辨識後的結果
				var json = JSON.parse(body);
				var asrResult = json['data']['asr']['result'];
				console.log("ASR Result = "+ asrResult);
				// if(callbackFunction) callbackFunction(asrResult);
				callbackFunction(asrResult);
			}
		});
	});
}

error結果:

/app/SpeechApiSample.js:152
callbackFunction(asrResult);
^
TypeError: callbackFunction is not a function
at IncomingMessage.<anonymous> (/app/SpeechApiSample.js:152:5)
at IncomingMessage.emit (events.js:203:15)
at endReadableNT (_stream_readable.js:1129:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
froce iT邦大師 1 級 ‧ 2019-07-23 11:34:25 檢舉
從你的code看不出你傳的 callbackFunction 這個參數是不是函數。
通常callback是你異步執行完後,會得到一個結果,你要再處理這個結果,所傳入的function。
Huiicat iT邦新手 4 級 ‧ 2019-07-23 11:37:09 檢舉
因為這是別人公開在網路上的教學範例,我照著他的步驟將原始碼做變動,最後卻發現他少給了callbackfunction的定義,好像只能自己產一個出來,但是沒什麼概念QQ
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

3
dragonH
iT邦超人 5 級 ‧ 2019-07-23 11:53:18
最佳解答

codepen

簡單的範例

你應該是參考這個的吧

可以看一下他怎麼去呼叫的

看更多先前的回應...收起先前的回應...
Huiicat iT邦新手 4 級 ‧ 2019-07-23 12:12:26 檢舉

謝謝,我研究看看!

Huiicat iT邦新手 4 級 ‧ 2019-07-23 13:28:41 檢舉

最後我比對了您提供的github程式碼,發現了前面function有新增幾行callbackfunction,補上後就解決問題了,謝謝!

dragonH iT邦超人 5 級 ‧ 2019-07-23 13:35:50 檢舉

重點應該是呼叫的時候有帶入

/images/emoticon/emoticon82.gif

Huiicat iT邦新手 4 級 ‧ 2019-07-23 13:51:54 檢舉

對的,因為我是看
https://tw.olami.ai/blog/article/40
一開始沒有仔細明白,但順著呼叫的function步驟走後,發現中間的function沒有帶入,再對照著看後就發現問題了~

我要發表回答

立即登入回答