我正在浏览wit.ai quickstart tutorial并从节点(版本4.5.0 LTS)收到错误,但我没有在代码中看到错误,这也是示例代码直接从wit.ai下载:
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
此外,从git下载的代码与教程页面上显示的代码不同:
来自git clone的代码,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);
});
},
};
无论如何,两个版本都会出现相同的错误。
答案 0 :(得分:1)
事实证明,由于代码中的某些ECMAScript 6功能,如果您的节点版本&lt; = 6.x.x,则必须将flags_destructuring标志添加到节点命令行,如下所示:
class CartesianProduct(product: Traversable[Traversable[_ <: Any]]) {
override def toString(): String = {
product.toString
}
def *(rhs: Traversable[_ <: Any]): CartesianProduct = {
val p = product.flatMap { lhs =>
rhs.map { r =>
lhs.toList :+ r
}
}
new CartesianProduct(p)
}
}
object CartesianProduct {
def apply(traversable: Traversable[_ <: Any]): CartesianProduct = {
new CartesianProduct(
traversable.map { t =>
Traversable(t)
}
)
}
}
// TODO: How can this conversion be made implicit?
val x = CartesianProduct(Set(0, 1))
val y = List("Alice", "Bob")
val z = Array(Math.E, Math.PI)
println(x * y * z) // Set(List(0, Alice, 3.141592653589793), List(0, Alice, 2.718281828459045), List(0, Bob, 3.141592653589793), List(1, Alice, 2.718281828459045), List(0, Bob, 2.718281828459045), List(1, Bob, 3.141592653589793), List(1, Alice, 3.141592653589793), List(1, Bob, 2.718281828459045))
// TODO: How can this conversion be made implicit?
val s0 = CartesianProduct(Seq(0, 0))
val s1 = Seq(0, 0)
println(s0 * s1) // List(List(0, 0), List(0, 0), List(0, 0), List(0, 0))