繁体   English   中英

节点 JS,我如何使用 ZCCADCDEDB567ABAE643E15DCF0974E503Z 从集合中获取数据

[英]Node JS, How do i get data from a collection with Mongoose

我正在使用 MVC。 如何在我的loginController中使用findOne({})方法?

我只想从我的收藏中获取数据。 此外,该系列已经存在一些东西。 我想要的只是从中获取数据。 顺便说一句,我为我的英语感到抱歉。

App.js

const dbURL = process.env.DB_URL;
mongoose.connect(dbURL, { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true })
        .then((result) => {console.log('bağlandı'); app.listen(8080);})
        .catch((err) => console.log(err))

loginController.js

const loginControllerPost = async (req, res) => {
        db.collection.findOne({req.body.username}) //How to properly use the method?
}

您需要定义模式(表)来告诉 mongoose 数据库结构是什么样的。 例如,用户架构:

const conn = mongoose.createConnection('your connection string');
const UserSchema = new Schema({
  name: String,
  username: String,
  password: String,
}, {
  collection: 'users', // define collection name here
});
const UserModel = conn.model('UserModel', UserSchema);
const user = await UserModel.findOne({ username: 'Alice' });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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