2016-12-05 39 views
0

我正在通過wit.ai quickstart tutorial並從節點(版本4.5.0 LTS)中獲取錯誤,但在代碼中看不到錯誤而且,它是直接從wit.ai下載示例代碼:wit.ai quickstart.js錯誤來自節點,意外的令牌{

examples\quickstart.js:39 
const {sessionId, context, entities} = request; 
SyntaxError: Unexpected token { 
    at exports.runInThisContext (vm.js:53:16) 
    at Module._compile (module.js:373:25) 
    at Object.Module._extensions..js (module.js:416:10) 
    at Module.load (module.js:343:32) 
    at Function.Module._load (module.js:300:12) 
    at Function.Module.runMain (module.js:441:10) 
    at startup (node.js:139:18) 
    at node.js:974:3 

而且,從混帳下載的代碼比教程頁面上顯示的代碼不同:

從混帳代碼克隆,example/quickstart.js是這樣的:

'use strict'; 

let Wit = null; 
let interactive = null; 
try { 
    // if running from repo 
    Wit = require('../').Wit; 
    interactive = require('../').interactive; 
} catch (e) { 
    Wit = require('node-wit').Wit; 
    interactive = require('node-wit').interactive; 
} 

const accessToken = (() => { 
    if (process.argv.length !== 3) { 
    console.log('usage: node examples/quickstart.js <wit-access-token>'); 
    process.exit(1); 
    } 
    return process.argv[2]; 
})(); 

// Quickstart example 
// See https://wit.ai/ar7hur/quickstart 

const firstEntityValue = (entities, entity) => { 
    const val = entities && entities[entity] && 
    Array.isArray(entities[entity]) && 
    entities[entity].length > 0 && 
    entities[entity][0].value 
    ; 
    if (!val) { 
    return null; 
    } 
    return typeof val === 'object' ? val.value : val; 
}; 

const actions = { 
    send(request, response) { 
    const {sessionId, context, entities} = request; 
    const {text, quickreplies} = response; 
    console.log('sending...', JSON.stringify(response)); 
    }, 
    getForecast({context, entities}) { 
    var location = firstEntityValue(entities, 'location'); 
    if (location) { 
     context.forecast = 'sunny in ' + location; // we should call a weather API here 
     delete context.missingLocation; 
    } else { 
     context.missingLocation = true; 
     delete context.forecast; 
    } 
    return context; 
    }, 
}; 

const client = new Wit({accessToken, actions}); 
interactive(client); 

但教程說const的動作部分應該是這樣的:

const actions = { 
    send(request, response) { 
    const {sessionId, context, entities} = request; 
    const {text, quickreplies} = response; 
    return new Promise(function(resolve, reject) { 
     console.log('sending...', JSON.stringify(response)); 
     return resolve(); 
    }); 
    }, 
    getForecast({context, entities}) { 
    return new Promise(function(resolve, reject) { 
     // Here should go the api call, e.g.: 
     // context.forecast = apiCall(context.loc) 
     context.forecast = 'sunny'; 
     return resolve(context); 
    }); 
    }, 
}; 

無論如何,這兩個版本給出了同樣的錯誤。

回答

1

事實證明,由於一些代碼的ECMAScript 6個特性,如果你的節點版本< = 6.x.x,您必須將標誌harmony_destructuring添加到節點命令行,如圖所示這裏:

node --harmony_destructuring examples/quickstart.js