简体   繁体   中英

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

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)
}) })

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM