简体   繁体   中英

NodeJS Express Function not defined

All I'm trying to do is execute geoAutocomplete in browser console so I can console log values from form. I'm getting ReferenceError: geoAutocomplete is not defined error.

const GooglePlaces = require('google-places');
const places = new GooglePlaces("myapikey");

function geoAutocomplete(input, lat, long) {
    console.log(input, lat, long);

    //const dropdown = places.autocomplete(input);
}

export default geoAutocomplete;

myApp.js

import '../sass/style.scss'
import geoAutocomplete from './modules/geoAutocomplete'

geoAutocomplete( document.querySelector('#address'), document.querySelector('#lat'), document.querySelector('#long') );

layout.pug

    script(src='/javascript/vcaApp.js' type='module')
form(method='POST' action='/addGeofence')
                    h3 Add a Geofence
                    div.input-field
                        label(for='address') Address
                        input(type='text' id='address', placeholder='Address',
                        required='true', autofocus='true' name='address')
                    div.input-field(style="display: none;")
                        input(type='number' id='lat', placeholder='Address',
                        autofocus='true' name='lat')
                    div.input-field(style="display: none;")
                        input(type='number' id='long', autofocus='true' name='long')

Just use module.exports / require . It's hard to tell where the error gets produced as you don't know how you're making the code runable in a browser (neither require nor import are supported there).

The easiest will be to just use

module.exports = getAutocomplete;

and to import the function in the other file

const geoAutocomplete = require('./modules/geoAutocomplete');

Your pre es6 compiler/transpiler/whatever will be able to handle this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM