2019-01-30に投稿

alexa-appでの実装なのですが。
node-fetchで別サーバからjsonを取得してres.say()で音声応答を返すのですが、
console.log()内ではjsonの内容を表示できるものの、
res.say()に渡すとstringじゃないよと怒られます。

const Alexa = require('alexa-app');
const app = new Alexa.app('Sample');
const fetch = require('node-fetch')


const averageResponse = (req, res) => {
  var num = req.slots['num'].value
  console.log(num)
  fetch('https://******',
    {
      method:'GET',
      headers: { 'Content-Type': 'application/json' }
    })
  .then((response)=>{
    if(response.status === 200){
      return response.json()
    }
  })
  .then((json)=>{
    console.log(json['averageTop10'][num - 1])
    //console.log(res)
    //var json = JSON.parse(data)
    try {
      if(num <= 10){
         //console.log(typeof(String(json.averageTop10[num - 1].average)))
        res.say(''+json.averageTop10[num - 1].average).shouldEndSession(true)
      }  

    } catch (error) {
      console.error(error)      
    }
  })
  .catch((err)=>{
    console.error(err);
  })

コメントアウトしてますが、String()コンストラクタやtoString()メソッドで型変換試してもエラーがキャッチできず、処理も正常に終わってないようなのでちょっと悩み中です。
↓このあたり怪しいなーとは思ってheaderはつけてみましたが変わらず。
str.replace is not a function


ckoshien

個人開発5年目。普段はフロントエンドエンジニア。 ReactJS/NextJS/NodeJS/ReactNative/Java

コメント

タグ

投稿月