I’m developing a Javascript script to retrieve delivery data from an SQL database and use it to create a delivery route using the Navixy API.
After connecting to the SQL database, a function retrieves delivery records from the SQL database’s Deliveries table. The retrieved data is then used to create a delivery route using the createDeliveryRoute function.
When I run my script, I get no error, but no route is created.
<code>const { createConnection } = require('mysql2/promise')
const axios = require('axios')
const USER_HASH = 'xxxxxx'
const TRACKER_ID = 123456
const createConnectionAndHost = async () => {
return await createConnection({
// Function to retrieve delivery data from the SQL database
const getFiles = async shipmentId => {
const host = await createConnectionAndHost()
const [sqlQuery] = await host.query(
'SELECT * FROM Delivery_test')
const values = [shipmentId];
return new Promise((resolve, reject) => {
dbConnection.query(sqlQuery, values, (error, results) => {
const createDeliveryRoute = async (deliveryData) => {
label: deliveryData.address, // Use the address as the label
description: `Livraison ${deliveryData.shipmentId}`, // Description with the delivery number
from: `${deliveryData.deliveryDate} 06:00:00`, // Start of the delivery window
to: `${deliveryData.deliveryDate} 18:00:00`, // End of the delivery window
lat: deliveryData.latitude,
lng: deliveryData.longitude,
radius: 100 // Default search radius
label: deliveryData.address, // Use the address as the label
external_id: deliveryData.shipmentId, // Delivery number as the external ID
max_delay: 0, // Maximum allowed delay
min_stay_duration: 10, // Minimum stay duration
create_form: false // Do not create a form for now
const createRoute = async (shipmentId) => {
const connection = await createConnectionAndHost();
const files = await getFiles(connection, shipmentId);
const requestBody = await createDeliveryRoute(files);
const response = await axios.post('https://api.navixy.com/v2/task/route/create', requestBody, { validateStatus: () => true });
console.error("An error occurred while creating the route: ", error);
module.exports = { createRoute }
<code>const { createConnection } = require('mysql2/promise')
const axios = require('axios')
const USER_HASH = 'xxxxxx'
const TRACKER_ID = 123456
const createConnectionAndHost = async () => {
return await createConnection({
host: 'localhost',
user: 'user',
password: '12345',
database: 'db_mydata',
})
}
// Function to retrieve delivery data from the SQL database
const getFiles = async shipmentId => {
const host = await createConnectionAndHost()
const [sqlQuery] = await host.query(
'SELECT * FROM Delivery_test')
const values = [shipmentId];
return new Promise((resolve, reject) => {
dbConnection.query(sqlQuery, values, (error, results) => {
if (error) {
reject(error);
} else {
resolve(results[0]);
}
});
});
}
const createDeliveryRoute = async (deliveryData) => {
const routeData = {
hash: USER_HASH,
route: {
tracker_id: TRACKER_ID,
label: deliveryData.address, // Use the address as the label
description: `Livraison ${deliveryData.shipmentId}`, // Description with the delivery number
from: `${deliveryData.deliveryDate} 06:00:00`, // Start of the delivery window
to: `${deliveryData.deliveryDate} 18:00:00`, // End of the delivery window
},
checkpoints: [{
location: {
lat: deliveryData.latitude,
lng: deliveryData.longitude,
radius: 100 // Default search radius
},
label: deliveryData.address, // Use the address as the label
external_id: deliveryData.shipmentId, // Delivery number as the external ID
max_delay: 0, // Maximum allowed delay
min_stay_duration: 10, // Minimum stay duration
}],
create_form: false // Do not create a form for now
};
return routeData
}
const createRoute = async (shipmentId) => {
try {
const connection = await createConnectionAndHost();
const files = await getFiles(connection, shipmentId);
const requestBody = await createDeliveryRoute(files);
const response = await axios.post('https://api.navixy.com/v2/task/route/create', requestBody, { validateStatus: () => true });
return response;
} catch (error) {
console.error("An error occurred while creating the route: ", error);
}
}
module.exports = { createRoute }
</code>
const { createConnection } = require('mysql2/promise')
const axios = require('axios')
const USER_HASH = 'xxxxxx'
const TRACKER_ID = 123456
const createConnectionAndHost = async () => {
return await createConnection({
host: 'localhost',
user: 'user',
password: '12345',
database: 'db_mydata',
})
}
// Function to retrieve delivery data from the SQL database
const getFiles = async shipmentId => {
const host = await createConnectionAndHost()
const [sqlQuery] = await host.query(
'SELECT * FROM Delivery_test')
const values = [shipmentId];
return new Promise((resolve, reject) => {
dbConnection.query(sqlQuery, values, (error, results) => {
if (error) {
reject(error);
} else {
resolve(results[0]);
}
});
});
}
const createDeliveryRoute = async (deliveryData) => {
const routeData = {
hash: USER_HASH,
route: {
tracker_id: TRACKER_ID,
label: deliveryData.address, // Use the address as the label
description: `Livraison ${deliveryData.shipmentId}`, // Description with the delivery number
from: `${deliveryData.deliveryDate} 06:00:00`, // Start of the delivery window
to: `${deliveryData.deliveryDate} 18:00:00`, // End of the delivery window
},
checkpoints: [{
location: {
lat: deliveryData.latitude,
lng: deliveryData.longitude,
radius: 100 // Default search radius
},
label: deliveryData.address, // Use the address as the label
external_id: deliveryData.shipmentId, // Delivery number as the external ID
max_delay: 0, // Maximum allowed delay
min_stay_duration: 10, // Minimum stay duration
}],
create_form: false // Do not create a form for now
};
return routeData
}
const createRoute = async (shipmentId) => {
try {
const connection = await createConnectionAndHost();
const files = await getFiles(connection, shipmentId);
const requestBody = await createDeliveryRoute(files);
const response = await axios.post('https://api.navixy.com/v2/task/route/create', requestBody, { validateStatus: () => true });
return response;
} catch (error) {
console.error("An error occurred while creating the route: ", error);
}
}
module.exports = { createRoute }
The requestBody data corresponds to the data expected by the API. But no results.
I’m interested in any ideas that might help.
Thanks