heroku + node.js + expressでLINE Messaging APIを使ってぐるなびAPIを呼べるLINEbotを作ろうとしています。
「TypeError: Cannot read property '0' of undefined」というエラーが出て困っています。
参考にしたページ:
https://qiita.com/enta0701/items/45d5295e023e24e9ff0b
「TypeError: Cannot read property '0' of undefined」というエラーが出て困っています。
以下が書いたコードです。
※エラーを出したと思われる箇所に「<<<<エラー」と記入しています。
Node.js
1// index.js 2 3'use strict'; 4 5var express = require('express'); 6var app = express(); 7var bodyParser = require('body-parser'); 8var request = require('request'); 9var async = require('async'); 10var crypto = require('crypto'); 11var line = require('@line/bot-sdk'); 12 13console.log("app is running"); 14 15// ぐるなび 16app.set('port', (process.env.PORT || 8000)); 17app.use(bodyParser.urlencoded({extended: true})); // JSONの送信を許可 18app.use(bodyParser.json()); // JSONのパースを楽に(受信時) 19 20app.listen(process.env.PORT || 8000); 21console.log("Server is online"); 22 23app.post('/callback', function(req, res){ 24 25 console.log("post() has been called"); 26 async.waterfall([ 27 // ぐるなびAPI 28 function(callback) { 29 30 var json = req.body; 31 32 // 受信テキスト 33 var search_place = json['result'][0]['content']['text']; <<<<<エラー 34 var search_place_array = search_place.split("\n"); 35 36 //検索キーワード 37 var gnavi_keyword = ""; 38 if(search_place_array.length == 2){ 39 var keyword_array = search_place_array[1].split("、"); 40 gnavi_keyword = keyword_array.join(); 41 } 42 43 // ぐるなびAPI レストラン検索API 44 var gnavi_url = 'http://api.gnavi.co.jp/RestSearchAPI/20150630/'; 45 // ぐるなび リクエストパラメータの設定 46 var gnavi_query = { 47 // "keyid":"<ぐるなびのアクセスキー>", 48 "keyid":"-----", 49 "format": "json", 50 "address": search_place_array[0], 51 "hit_per_page": 1, 52 "freeword": gnavi_keyword, 53 "freeword_condition": 2 54 }; 55 var gnavi_options = { 56 url: gnavi_url, 57 headers : {'Content-Type' : 'application/json; charset=UTF-8'}, 58 qs: gnavi_query, 59 json: true 60 }; 61 62 // 検索結果をオブジェクト化 63 var search_result = {}; 64 65 request.get(gnavi_options, function (error, response, body) { 66 if (!error && response.statusCode === 200) { 67 if('error' in body){ 68 console.log("検索エラー" + JSON.stringify(body)); 69 return; 70 } 71 72 // 店名 73 if('name' in body.rest){ 74 search_result['name'] = body.rest.name; 75 } 76 // 画像 77 if('image_url' in body.rest){ 78 search_result['shop_image1'] = body.rest.image_url.shop_image1; 79 } 80 // 住所 81 if('address' in body.rest){ 82 search_result['address'] = body.rest.address; 83 } 84 // 緯度 85 if('latitude' in body.rest){ 86 search_result['latitude'] = body.rest.latitude; 87 } 88 // 経度 89 if('longitude' in body.rest){ 90 search_result['longitude'] = body.rest.longitude; 91 } 92 // 営業時間 93 if('opentime' in body.rest){ 94 search_result['opentime'] = body.rest.opentime; 95 } 96 97 callback(null, json, search_result); 98 99 } else { 100 console.log('error: '+ response.statusCode); 101 } 102 }); 103 104 }, 105 ], 106 107 // LINE BOT 108 function(err, json, search_result) { 109 if(err){ 110 return; 111 } 112 113 //ヘッダーを定義 114 var headers = { 115 'Content-Type' : 'application/json; charset=UTF-8', 116 'X-Line-ChannelID' : '1558387552', 117 'X-Line-ChannelSecret' : '-----', 118 'X-Line-Trusted-User-With-ACL' : '-----' 119 }; 120 121 // 送信相手の設定(配列) 122 var to_array = []; 123 to_array.push(json['result'][0]['content']['from']); 124 125 126 // 送信データ作成 127 var data = { 128 'to': to_array, 129 'toChannel': 1383378250, //固定 130 'eventType':'140177271400161403', //固定 131 "content": { 132 "messageNotified": 0, 133 "messages": [ 134 // テキスト 135 { 136 "contentType": 1, 137 "text": 'こちらはいかがですか?\n【お店】' + search_result['name'] + '\n【営業時間】' + search_result['opentime'], 138 }, 139 // 画像 140 { 141 "contentType": 2, 142 "originalContentUrl": search_result['shop_image1'], 143 "previewImageUrl": search_result['shop_image1'] 144 }, 145 // 位置情報 146 { 147 "contentType":7, 148 "text": search_result['name'], 149 "location":{ 150 "title": search_result['address'], 151 "latitude": Number(search_result['latitude']), 152 "longitude": Number(search_result['longitude']) 153 } 154 } 155 ] 156 } 157 }; 158 159 //オプションを定義 160 var options = { 161 url: 'https://trialbot-api.line.me/v1/events', 162 // proxy : process.env.FIXIE_URL, 163 proxy : '-----', 164 headers: headers, 165 json: true, 166 body: data 167 }; 168 console.log(process.env.FIXIE_URL); 169 170 request.post(options, function (error, response, body) { 171 if (!error && response.statusCode == 200) { 172 console.log(body); 173 } else { 174 console.log('error: '+ JSON.stringify(response)); 175 } 176 }); 177 }); 178}); 179 180//署名検証 181function validate_signature(signature, body) { 182 return signature === crypto.createHmac('sha256', process.env.LINE_CHANNEL_SECRET).update(new Buffer(JSON.stringify(body), 'utf8')).digest('base64'); 183} 184 185 186 187
Procfile
1// Procfile 2 3web: node index.js
Package.json
1// Package.json 2 3{ 4 "name": "ciecho", 5 "version": "1.0.0", 6 "description": "[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy)", 7 "main": "index.js", 8 "dependencies": { 9 "@line/bot-sdk": "^5.2.0", 10 "async": "^2.6.0", 11 "babel-cli": "^6.26.0", 12 "body-parser": "^1.18.2", 13 "crypto": "^1.0.1", 14 "express": "^4.16.2", 15 "http": "0.0.0", 16 "mongodb": "^3.0.1", 17 "request": "^2.83.0" 18 }, 19 "devDependencies": {}, 20 "scripts": { 21 "start": "node ." 22 }, 23 "engines": { 24 "node": "8.9.3", 25 "npm": "5.5.1" 26 }, 27 "repository": { 28 "type": "git", 29 "url": "git+https://github.com/chikaichikai/ciecho.git" 30 }, 31 "author": "cici", 32 "license": "ISC", 33 "bugs": { 34 "url": "https://github.com/chikaichikai/ciecho/issues" 35 }, 36 "homepage": "https://github.com/chikaichikai/ciecho#readme", 37 "keywords": [ 38 "donut" 39 ] 40} 41
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/01/28 03:24 編集
2018/01/28 03:26
退会済みユーザー
2018/01/28 03:41 編集
2018/01/28 03:44
退会済みユーザー
2018/01/28 03:59 編集
2018/01/28 04:27
退会済みユーザー
2018/01/28 06:05