I have logged the dbSong and results are satisfying, but they are not being saved into database.
The schema and all is like this.
const baseURL = "https://songspk.mobi";
mongoose.connect("mongodb://localhost:27017/songsDB", {useNewUrlParser: true, useUnifiedTopology: true});
//New Song Schema
const songSchema = new mongoose.Schema({
name: String,
parent_url: String,
child_url: String,
album: String,
duration: String,
singers: String,
lyricist: String,
music_director: String,
download_128: String,
download_320: String,
image_url: String
});
const Song = mongoose.model("Song", songSchema);
This is distribution of work.
if(cluster.isMaster) {
console.log(`I'm Master ${process.pid}`);
for(let i=0;i<numCPUs;i++) { //Forking all 8 CPUs
cluster.fork();
}
} else {
const wokerID = cluster.worker.id;
fetchResults(links[wokerID-1]);
fetchResults(links[wokerID + 7]);
fetchResults(links[wokerID + 15]);
if(wokerID === 1) {
fetchResults(links[24]);
}
if(wokerID === 2) {
fetchResults(links[25]);
}
//console.log(`I'm worker ${process.pid}`);
process.exit();
}
I'm using cluster for higher performance. So, I have started calling functions on each CPU, and it's working. The work is now very fast, almost 8 times and it's logging what it is doing. Each CPU is working parallelly and calling a function names fetchResults(), which is fecthing some results from a website.
Now inside fetchResults() function, a link has been passed and it goes to a web page and fetches all the song details, let say 25 songs on a link.
function fetchResults(link) {
const result = makeGetRequest(link);
const dom = new jsdom.JSDOM(result);
const numberOfSongs = dom.window.document.querySelectorAll(".thumb-image a[href]").length;
for(var i=0;i<numberOfSongs;i++) { //Particular Song
const song = {
name: "",
parent_url: link,
child_url: "",
album: "",
duration: "",
singers: "",
lyricist: "",
music_director: "",
download_320: "",
download_128: "",
image_url: ""
};
// I have put all songs details in this song object. Now I want to save this into my
//database
const dbSong = new Song({
name: song.name,
parent_url: song.parent_url,
child_url: song.child_url,
album: song.album,
duration: song.duration,
singers: song.singers,
lyricist: song.lyricist,
music_director: song.music_director,
download_128: song.download_128,
download_320: song.download_320,
image_url: song.image_url
});
//console.log(dbSong);
dbSong.save(function(err){
if(err) {
console.log(err);
} else {
console.log("*********I did that********");
}
});
}
}
Now if I log dbSong or song it shows everything on console, but nothing error or Success message in console. And also, it is not saving any data into database. Please help me. If I save data outside the function it saves that correctly, but not inside the function which contains a loop. I'm not getting why it is so, may me something like sync or async, please help me!
When I tried to use
const promise = dbSong.save();
console.log(promise);
I got result on log as:
Promise { <pending> }