【问题标题】:How to send an object that I read from MongoDB to node js to another js file?如何将我从 MongoDB 读取到节点 js 的对象发送到另一个 js 文件?
【发布时间】:2022-01-26 12:45:53
【问题描述】:

如何将我从 MongoDB 读取到节点 js 的对象发送到另一个 js 文件?

我想将 arr 发送到另一个 js 文件。

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

【问题讨论】:

    标签: javascript node.js mongodb


    【解决方案1】:

    如果您想要客户端的数据,您可以创建一个api路由并将数据作为json发送。

    如果你只是想要另一个 js 文件中的数据,那么你可以只需要文件中的 db 模型并使用 db find 来使用那里的结果。

    【讨论】:

      猜你喜欢
      • 2019-01-16
      • 2021-07-07
      • 2022-11-04
      • 1970-01-01
      • 1970-01-01
      • 2013-04-26
      • 1970-01-01
      • 2021-01-24
      • 2021-08-22
      相关资源
      最近更新 更多