-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixId.js
45 lines (36 loc) · 1.13 KB
/
fixId.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const Fs = require("fs");
const Path = require("path");
const Axios = require("axios");
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const PokedexImgSchema = new Schema({
imageUrl: { type: "String", required: true },
number: { type: "String", required: true },
artist: { type: "String", required: true },
filePath: { type: "String", required: true, unique: true },
id: { type: "Number", required: true}
});
let PokemonImg = mongoose.model("PokeDex", PokedexImgSchema);
mongoose.connect("mongodb://localhost:27017/pokemontcgimages");
async function downloadImage(url, filename) {
const path = Path.resolve(__dirname, `arts/`, `${filename}.png`);
const writer = Fs.createWriteStream(path);
const response = await Axios({
url,
method: "GET",
responseType: "stream",
});
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on("finish", resolve);
writer.on("error", reject);
});
}
(async () => {
let pokemons = await PokemonImg.find();
for (pokemon of pokemons){
pokemon.id = parseInt(pokemon.number)
pokemon.save()
}
console.log("acabou");
})();