const axios = require('axios'); // const host = 'http://localhost:3000'; const host = 'https://definma-api.apps.de1.bosch-iot-cloud.com'; let errors = []; async function fix() { let res = await axios({ method: 'get', url: host + '/samples?status[]=new&status[]=validated&status[]=deleted&fields[]=_id&fields[]=number', auth: { username: 'admin', password: 'Abc123!#' } }).catch(err => { if (err.response) { console.error(err.response.data); errors.push(`Could not fetch samples: ${JSON.stringify(err.response.data)}`); } }); console.log(res.data); // for all samples for (let sampleIndex = 2000; sampleIndex < res.data.length; sampleIndex++) { console.log(`SAMPLE ${sampleIndex}/${res.data.length}`); const measurements = await axios({ // get all measurements method: 'get', url: host + '/measurement/sample/' + res.data[sampleIndex]._id, auth: { username: 'admin', password: 'Abc123!#' } }).catch(err => { if (err.response) { console.error(err.response.data); errors.push(`Could not fetch measurements: ${JSON.stringify(err.response.data)}`); } }); if (measurements && measurements.data) { // found measurements for (let measurementIndex in measurements.data) { console.log(`${measurementIndex}/${measurements.data.length}`); const measurement = measurements.data[measurementIndex]; if (measurement.values.hasOwnProperty('dpt') && measurement.values.dpt) { // is spectrum await axios({ // get all measurements method: 'put', url: host + '/measurement/' + measurement._id, auth: { username: 'admin', password: 'Abc123!#' }, data: {values: {dpt: measurement.values.dpt .filter(e => e.length === 2) .map(e => e.map(el => parseFloat(el))) }} }).catch(err => { if (err.response) { console.error(err.response.data); errors.push(`Could not save measurements: ${JSON.stringify(err.response.data)}`); } }); } } } } console.log(errors); } fix();