javascript - How do I use .populate with .findOne in an Express route? - Stack Overflow

I'm in the process of learning Node, Mongoose and Express by trying to build a CRUD API. When tryi

I'm in the process of learning Node, Mongoose and Express by trying to build a CRUD API. When trying to "join" two MongoDB collections with the .populate function, I get an error that

db.collection(...).findOne(...).populate is not a function

My javascript chops are not great, so I've tried rewriting this in different ways to no avail.

server.js

require('./models/User')
require('./models/Product')

var db 

mongodb.MongoClient.connect('mongodb://<username>:</password>@xxxx.mlab:xxx/xxx', (err, database) => {
if (err) return console.log(err)
    db = database
    app.listen(3000, function() {
        console.log("listening on port 3000");
    })
})

app.get('/api/users/:id', (req, res) => {
db.collection(USERS_COLLECTION).findOne({ id: (req.params.id) }).populate('product'), (err, doc) => {
    if (err) handleError(res, err.message, 'Failed to get user')

    res.status(200).json(doc)   
    console.log(req.params)
 }
})

models/user.js

 var mongoose = require('mongoose')

 var UserSchema = new mongoose.Schema({
 id: String,
 first_name: String,
 last_name: String
 id: {
 type: mongoose.Schema.Types.ObjectId,
 ref: 'Product'
 }
})

mongoose.model('User', UserSchema)

I'm in the process of learning Node, Mongoose and Express by trying to build a CRUD API. When trying to "join" two MongoDB collections with the .populate function, I get an error that

db.collection(...).findOne(...).populate is not a function

My javascript chops are not great, so I've tried rewriting this in different ways to no avail.

server.js

require('./models/User')
require('./models/Product')

var db 

mongodb.MongoClient.connect('mongodb://<username>:</password>@xxxx.mlab.:xxx/xxx', (err, database) => {
if (err) return console.log(err)
    db = database
    app.listen(3000, function() {
        console.log("listening on port 3000");
    })
})

app.get('/api/users/:id', (req, res) => {
db.collection(USERS_COLLECTION).findOne({ id: (req.params.id) }).populate('product'), (err, doc) => {
    if (err) handleError(res, err.message, 'Failed to get user')

    res.status(200).json(doc)   
    console.log(req.params)
 }
})

models/user.js

 var mongoose = require('mongoose')

 var UserSchema = new mongoose.Schema({
 id: String,
 first_name: String,
 last_name: String
 id: {
 type: mongoose.Schema.Types.ObjectId,
 ref: 'Product'
 }
})

mongoose.model('User', UserSchema)
Share Improve this question edited Feb 20, 2017 at 19:51 Bardo_J asked Feb 20, 2017 at 19:21 Bardo_JBardo_J 511 gold badge1 silver badge7 bronze badges 3
  • Which node module is db? – Sangharsh Commented Feb 20, 2017 at 19:22
  • If you are using Mongoose then you can use schema methods so you should be able to use .populate on a model object – Explosion Pills Commented Feb 20, 2017 at 19:26
  • @Sangharsh I've added how i'm setting up the db to the top of the code – Bardo_J Commented Feb 20, 2017 at 19:29
Add a ment  | 

1 Answer 1

Reset to default 2

Collections don't have the mongoose's model API including .populate. Instead of using collections directly, you should use the model that you registered in mongoose.

var schema = new mongoose.Schema(...);

var User = mongoose.model('User', schema);
// you can also get the model after it was registered via mongoose.model('User')

app.get('/api/users/:id', (req, res) => {
User.findOne({ id: (req.params.id) }).populate('product'), (err, doc) => {
    if (err) handleError(res, err.message, 'Failed to get user')

    res.status(200).json(doc)   
    console.log(req.params)
 }
})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745291282a4620875.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信