const translate = require('translate-api');
let transText = 'hello world';
translate.getText(transText, { to: 'en' }).then(function(text) {
console.log(text)
}).catch(err => {
console.log(err) // {Error code: 'BAD_NETWORK' }
});
真实错误信息是
TypeError: Cannot read property '0' of null
api.js修改成这样就好了
// if (body[5] !== undefined && body[5][0] !== undefined && body[5][0][0] !== undefined) {
if (body[5] && body[5][0] && body[5][0][0]) {
let str = body[5][0][0];
str = str.replace(/<b><i>/g, '[');
str = str.replace(/<\/i><\/b>/g, ']');
result.from.text.value = str;
if (body[5][0][5] === 1) {
result.from.text.autoCorrected = true;
} else {
result.from.text.didYouMean = true;
}
}
直接判断是否存在就可以
真实错误信息是
api.js修改成这样就好了
直接判断是否存在就可以