C
C
CODER5412018-02-28 20:25:28
MongoDB
CODER541, 2018-02-28 20:25:28

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)

Where did I screw up?
Here is the code:
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

1 answer(s)
V
Vladimir Skibin, 2018-02-28
@CODER541

const database = require('./quotebase')

It seems to be clearly written, you are trying to call a method on the property of an object that was pulled out of JSON.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question