You can check whether site collection exists or not as below:
1) Add the below import statement, we will be using the SPHttpClient provided by PnPJS to make a REST call to /_api/SP.Site.Exists
endpoint to determine if a site collection exists or not
import { sp } from "@pnp/sp";
import { SPHttpClient } from "@pnp/sp";
2) Modify your code as below, I created a helper method check if site collection exists or not and also used the render method, you can change it as per your requirement.:
public fetchData(siteUrl: string) : Promise<any> {
return new Promise(function (resolve, reject) {
var client = new SPHttpClient();
client.post("https://myspsite.net/_api/SP.Site.Exists", {
body: JSON.stringify({
url: siteUrl
}),
headers: {
"accept": "application/json;",
},
}).then(d => {
d.json().then((v: any) => {
resolve(v.value);
})
}).catch(d => {
reject(d);
});
});
}
public async render() {
var sitesToCheck = [
'https://myspsite.net/sites/sc1',
'https://myspsite.net/sites/sc2',
'https://myspsite.net/sites/sc3'
];
for (var i = 0; i < sitesToCheck.length; i++) {
await this.fetchData(sitesToCheck[i]).then(d => {
if(d){
console.log("Site collection " + sitesToCheck[i] + " exists");
}else{
console.log("Site collection " + sitesToCheck[i] + " doesnt exist");
}
});
}
}