Answer the question
In order to leave comments, you need to log in
How to read file line by line and write to database?
Hello, I am writing a telegram bot and I need to fill the database with quotes, I know how to fill the database. but I want to automate the process somehow. And I decided to create a text file, and write each quote into it line by line and read it all line by line and score it in the database. But the problem arose during the "compilation" gives me an error that:
Approx. ( quotes the name of the database, database is: const database = require('./quotebase')
Where "quotebase"
is the name of the file .json
)
i=6 because 5 elements are already in the database
database.quotes.insertOne(
^
TypeError: database.quotes.insertOne is not a function
at data.split.forEach.line (D:\myTelegrambot\index.js:100:23)
at Array.forEach (<anonymous>)
at fs.readFile (D:\myTelegrambot\index.js:97:22)
at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:528:3)
var i=6;
fs.readFile(QuoteUrl, { encoding : 'utf8' },(err, data) => {
if (err) throw err;
data.split('\n').forEach(line => {
var QuotTXTLine = line;
database.quotes.insertOne(
{
text: QuotTXTLine,
id:i
}
)
i=i+1;
});
});
Answer the question
In order to leave comments, you need to log in
const database = require('./quotebase')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question