0

How to send an object that I read from MongoDB to node js to another js file?

I want to send arr to another js file.

 MongoClient.connect(connectionURL, { useNewUrlParser: true }, (error,
 client) => {
     if (error) {
         return console.log("Can't connect to db");
}
const db = client.db(databaseName); jasonArray0 = [
    {
        opentime: 2000,
        image: "cat.jpg"
    }
];
db.collection('stam').insertMany(jasonArray0, (error, result) => {
    if (error) {
        return console.log('Could not insert')
    }
    console.log(result.ops)
})
db.collection('stam').findOne({
    _id: new mongodb.ObjectID("61ca52a727dc9d8f089d04d6")
}, (error, task) => {
    arr.push(task.image)
}) })

1 Answer 1

1

If you want the data on the client side, you can create an api route and send the data as json.

If you just want the data in another js file, then you can just require the db model in the file and use db find to use the result there.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.