Now I am develop a google chrome extension plugin, I define a translate function in the my-ts.js file like this(all the code running in Google Chrome extension):
import * as tjs from 'translation.js'
export default {
translate(queryObj) {
const api = tjs[queryObj.api]
return tjs[queryObj.api].translate(queryObj)
},
audio(queryObj) {
return tjs[queryObj.api].audio(queryObj)
}
}
when I use the function in another js file like this:
import {createServer} from 'connect.io';
import chromeCall from 'chrome-call';
import ts from '../public/my-ts';
import {write} from '../public/clipboard';
import ga from '../public/ga';
const server = createServer();
/**
* fetch the translate result
* @param {Query} queryObj
* @param {Function} resolve
*/
export async function onGetTranslateResult( queryObj , resolve ) {
queryObj.api = queryObj.api.toLowerCase()
if(queryObj.api === 'googlecn') {
queryObj.api = 'google'
} else if (queryObj.api === 'google') {
queryObj.api = 'google'
queryObj.com = true
}
const {api} = queryObj;
ga( 'send' , 'event' , 'translate' , api );
try {
resolve( await ts.translate( queryObj ) );
ga( 'send' , 'event' , 'translate success' , api );
}
catch ( errorMsg ) {
let error = errorMsg.message;
if (errorMsg.code === 'NETWORK_TIMEOUT') {
error = ''
}
if ( 'google' === queryObj.api && !queryObj.com ) {
error += 'tips: make sure you are not open the proxy';
}
ga( 'send' , 'event' , 'translate failed' , api );
resolve( { error, code: errorMsg.code } );
}
}
shows error:
Cannot read properties of undefined (reading 'translate')
why could not invoke the translate function? what should I do to make it work?