Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
Miloš Lapiš
@mlc-mlapis
let facility = JSON.parse(data); // Your code line
for (let i = 0; i < facility.length; i++) {
   const element = facility[i];
   if (!element.Type) {
      // The element doesn't contain property of Type
   }
}
Aishasadat123
@Aishasadat123
@mlc-mlapis please edit my code
and i want to check if there is not type proprty it have to add the type property
Miloš Lapiš
@mlc-mlapis
Better naming:
let facilities = JSON.parse(data); // Your code line
for (let i = 0; i < facilities.length; i++) {
   const facility = facilities[i];
   if (!facility.Type) {
      // The element doesn't contain property of Type
      facility.Type = 'NPM';
   } else {
      console.log('... data type:', typeof facility.Type); // Would be a string data type
   }
}
Aishasadat123
@Aishasadat123
@mlc-mlapis in the (if ) how i say that type to those object ?
Miloš Lapiš
@mlc-mlapis
@Aishasadat123 But be careful because fs.readFile is an async operation and fs.writeFileSync` is a sync operation, and you call that sync operation outside of the async body.
Aishasadat123
@Aishasadat123
'use strict';

const fs = require('fs');

fs.readFile('facilities.json', (err, data) => {
    if (err) throw err;

    let facilities = JSON.parse(data); // Your code line
    for (let i = 0; i < facilities.length; i++) {
        const facility = facilities[i];
        if (!facility.Type) {
            // The element doesn't contain property of Type
            facility.Type = 'NPM';
        } else {
            console.log('... data type:', typeof facility.Type); // Would be a string data type
        }
    }
    console.log(facilities);
});

let data = JSON.stringify(facilities);
fs.writeFileSync('facility-2.json', data);
Miloš Lapiš
@mlc-mlapis
@Aishasadat123 Then fs.readFileSync would be necessary or placing writing into the body of that async.
Aishasadat123
@Aishasadat123
my final code
it shows an error that facilities in 15 is not define
let data = JSON.stringify(facilities);
Miloš Lapiš
@mlc-mlapis
@Aishasadat123 It can't work correctly because you have fs.writeFileSync outside of the async body.
@Aishasadat123 Sure because the declaration of facilities is inside the async block.
fs.readFile('facilities.json', (err, data) => {
    if (err) throw err;

    const facilities = JSON.parse(data); // Your code line
    for (let i = 0; i < facilities.length; i++) {
        const facility = facilities[i];
        if (!facility.Type) {
            // The element doesn't contain property of Type
            facility.Type = 'NPM';
        } else {
            console.log('... data type:', typeof facility.Type); // Would be a string data type
        }
    }
    console.log(facilities);

    const outputData = JSON.stringify(facilities);
    fs.writeFileSync('facility-2.json', outputData);
});
Aishasadat123
@Aishasadat123
now it shows this error
SyntaxError: Identifier 'data' has already been declared
Miloš Lapiš
@mlc-mlapis
@Aishasadat123 Sure, use outputData.
Aishasadat123
@Aishasadat123
ok
@mlc-mlapis it find the facility with type NPM
it just find thefacility with NPM type not add anything
Miloš Lapiš
@mlc-mlapis
@Aishasadat123 Try facility['Type'] = 'NPM'; instead of facility.Type = 'NPM';
Aishasadat123
@Aishasadat123
not working
the result is same
Miloš Lapiš
@mlc-mlapis
@Aishasadat123 Add:
if (!facility.Type) {
   // The element doesn't contain property of Type
   facility.Type = 'NPM';
   console.log('... adding Type property:', facility);
} else {
   console.log('... data type:', typeof facility.Type); // Would be a string data type
}
Aishasadat123
@Aishasadat123
@mlc-mlapis still same result
Miloš Lapiš
@mlc-mlapis
@Aishasadat123 What is shown on the console for facility then?
Aishasadat123
@Aishasadat123
it show all facilities with type property
it does not make any change in the facility which does not has type property
Miloš Lapiš
@mlc-mlapis
@Aishasadat123 I mean the result of: console.log('... adding Type property:', facility);
Aishasadat123
@Aishasadat123
... data type: string
... adding Type property
Miloš Lapiš
@mlc-mlapis
@Aishasadat123 Nothing for both syntax: facility.Type = 'NPM'; or facility['Type'] = 'NPM';?
Aishasadat123
@Aishasadat123
it show this
for both show same result
Miloš Lapiš
@mlc-mlapis
@Aishasadat123 console.log('... adding Type property:', facility); shows only ... adding Type property: and nothing for facility?
@Aishasadat123 Then print also:
const facility = facilities[i];
console.log('... original content:', facility);
Aishasadat123
@Aishasadat123
it show facilities
Miloš Lapiš
@mlc-mlapis
@Aishasadat123 And after adding facility['Type'] = 'NPM'; it shows nothing?
Aishasadat123
@Aishasadat123
when i run the it also shows the facilities
i think i made you confused
can you please run it on your pc ?
if you want i will send the json file
ok?
Miloš Lapiš
@mlc-mlapis
OK. I can do it later. So put that JSON example here.
Aishasadat123
@Aishasadat123
ok
can i send in private message ?
santoshfurtado
@santoshfurtado

Hi Guys,

Could you please help me on the angular SSR with base ref deployment issue?
I am able to load the application without SSR with base ref but I am getting below issue with SSR & base href

Error : Failed to load module script: The server responded with a non-JavaScript MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec

I have also tried setting the type to type="text/javascript" but it also throws Unexpected token <

01.png
02.png
Ashref
@Asher2016
hello , i have issue when router.navigate(['../']) doesn't work because i have object/iframe loading html page