Client Wait On Server Call

I’ve tried the following method, but my client code does not wait on the server call to run. I’ve tried returning the result within the API Call, and I’ve put it outside of it at the end of the method. Nothing has worked. The client side code continues to run without waiting on the server call to finish.

I have this call in the client:
Meteor.call(‘decodeVin’, function (error, response) {
** // Code to be run after Meteor call finishes**
});

Here’s my how it’s set up on the server:

decodeVin: function(vin){
//api stuff
var axios = require(‘axios’).default;
axios.get(${apiURL}${vin}${urlTail})
.then(response => {

var vinData = JSON.parse(JSON.stringify(response));


console.log(response.data);

})
.catch(error => console.error(‘On get vin error’, error)).finally(response =>{

})

},

What is your Meteor version?
You have a mix there of sync and async functions/methods.

Also you need to return your axios.get. You call it but your method doesn’t have a return.
You need to go all async (Meteor > 2.12) or need to wrapAsync your function in the method or bind Meteor with Meteor.bindEnvironment(function())

Thanks for the reply! This app is on 2.6 just because it’s a pain to update app store apps. If I update to the latest release, how would this work?

It would be interesting to know what you are trying to get server side. If you have access to your apiURL and can manage CORS and you call from the server just to then send the info the client, it is best to fetch it directly on the client.

On the server it is best to do POST and leave it but GETs can affect user or server experience if the API you call is slow or buggy.
You have an example here: fetch | Meteor API Docs
From your async function of your method you would return the result of the last line (in the documentation)

You might also want to use AbortController() to abort the operation afte x seconds such as:

import { Meteor } from 'meteor/meteor';
import { fetch, Headers } from 'meteor/fetch';
import { fetch } from 'meteor/fetch'
import AbortController from 'abort-controller'

 const controller = new AbortController()
 const timeout = setTimeout(() => {
    controller.abort()
  }, 1000) // cancel call after 1 second

async function postData (url, data) {
    try {
      const response = await fetch(url, {
          signal: controller.signal
          method: 'POST', // *GET, POST, PUT, DELETE, etc.
          mode: 'cors', // no-cors, *cors, same-origin
          cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
          credentials: 'same-origin', // include, *same-origin, omit
          headers: new Headers({
              Authorization: 'Bearer my-secret-key',
              'Content-Type': 'application/json'
          }),
          redirect: 'follow', // manual, *follow, error
          referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
          body: JSON.stringify(data) // body data type must match "Content-Type" header
      });
      const data = await response.json();
      return response(null, data);
    } catch (err) {
      return response(err, null);
    }
}

const postDataCall = Meteor.wrapAsync(postData);
return postDataCall('https://www.example.org/statsSubmission', { totalUsers: 55 }))


I thought you couldn’t do that because of CORS. How would you manage an CORS with an API call from the client?

This is why I mentioned "If you have access to your apiURL and can manage CORS ". You can for instance proxy the API or pull it through the same domain and then you are on the same origin, referrer, domain. The point is that the API has to accept a call from the origin and if you have access to both caller and “callee” aka the API, you can make them talk with no issues.
In general the CORS problem on the client is when the API is not yours and you have no way to authorize yourself.
This discussion explains it and offers some solutions too: Getting 'Cross-Origin Request Blocked' on a GET request · Issue #853 · axios/axios · GitHub