How to select document by id with Sails-mongo?

后端 未结 3 854
谎友^
谎友^ 2021-01-22 22:09
User.find({ _id: { \'!\': user.id } }, function foundFriends (err, friends) {
    if(err) return next(err);
        res.view({
            friends: friends
        });
}         


        
相关标签:
3条回答
  • 2021-01-22 22:13

    There are couple of things I see are weird with your code.
    1. The callback function as for sails 10 should be executed with .exec()
    2. I think you should not search for _id but only id. I think waterline parses this underscore.

    Having that in mind your code should look something like

    User.find({ id: { '!': user.id } }).exec(function (err, friends) {
        if(err) return next(err);
        res.view({
            friends: friends
        });
    });
    
    0 讨论(0)
  • 2021-01-22 22:17

    You can try this:

    User.findOne({ _id : ObjectId(user.id.toString()) })
    

    Hope this help.

    0 讨论(0)
  • 2021-01-22 22:19

    Hi you can try this for find, update, or destroy by id with sails-mongo via select:

     //Schema
      module.exports = {  
       autoPK : false,
         attributes : {
           id : {
             type: 'string',
             primaryKey: true
           },
           name : {
             type : 'string'
           },
           email : {
             type : 'string'
           }
        }
     }
    
    
    
     // Find
     User.find({
       select : {id : user.id}
     }).exec((err,record)=> { 
       if(err) return console.log(err,"err");
       console.log(record,"record");
     })
    
     // Update
     User.update({
       select : {id : user.id}
     },{ 
      name : "Foo"
     }).exec((err,record)=> { 
       if(err) return console.log(err,"err");
       console.log(record,"record");
     })
    
     // Destroy
     User.destroy({
       select : {id : user.id}
     }).exec((err,record)=> { 
       if(err) return console.log(err,"err");
       console.log(record,"record");
     })
    

    Hope this can help to you.

    0 讨论(0)
提交回复
热议问题