TypeError: Cannot read properties of undefined (reading 'push') when trying to push to a mongoDB

Viewed 19

This is what the code looks like:

import { MongoClient } from 'mongodb';
import { config } from 'dotenv';
config();

export async function connectToCluster() {
    let mongoClient;
    const dbOptions; //Assume that this exists.
    mongoClient = new MongoClient(process.env.MONGO_URI, dbOptions);
    await mongoClient.connect();
    return mongoClient;
}

db = await connectToCluster();
temp = db.db('Database').collection('data').find({}).limit(1).database.push(JSON.parse(data));

This gives the aforementioned error even though "database" is a field in the collection "data" in the db "Database". Console-logging the entire thing up to .database gives undefined.

0 Answers
Related