-
Notifications
You must be signed in to change notification settings - Fork 0
/
uploder.js
37 lines (28 loc) · 908 Bytes
/
uploder.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
import mongoose from "mongoose";
import fs from "fs";
import readline from "readline";
import Meme from "./model.js";
const uplodeFile = async () =>{
const filePath = './imagelink.txt';
const readStream = fs.createReadStream(filePath);
const rl = readline.createInterface({
input: readStream,
crlfDelay: Infinity
});
const memeData = [];
rl.on('line', line => {
memeData.push({ imageUrl: line.trim() });
});
rl.on('close', async () => {
memeData.forEach(async (meme, index) => {
try {
const newMeme = new Meme({ name: `meme${index + 1}`, imageUrl: meme.imageUrl });
// Save the meme to the database
await newMeme.save();
console.log(`Meme ${index + 1} inserted successfully.`);
} catch (error) {
console.error(`Error inserting meme ${index + 1}:`, error);
}
});
})};
export default uplodeFile;