Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb/mongoose insert is not a function

My insert is not working, I got error of Error :

Token.insert is not a function

var Token = module.exports = mongoose.model('tokens', tokenSchema);

//error
module.exports.saveToken = function(owner_id, token, callback){
    console.log(owner_id,token);
    Token.insert({"owner":owner_id,"token":token},callback);
}
//working
module.exports.getAllTokens = function(owner_id, callback){
    Token.find({"owner":owner_id},callback);
}
like image 512
Alicia Brandon Avatar asked Jun 08 '16 08:06

Alicia Brandon


People also ask

What does __ V mean in Mongoose?

The __v field is called the version key. It describes the internal revision of a document. This __v field is used to track the revisions of a document. By default, its value is zero ( __v:0 ).

What does Mongodb insert return?

Returns: A document containing: A boolean acknowledged as true if the operation ran with write concern or false if write concern was disabled. A field insertedId with the _id value of the inserted document.

Can we use findOne in Mongoose?

Mongoose | findOne() FunctionThe findOne() function is used to find one document according to the condition. If multiple documents match the condition, then it returns the first document satisfying the condition.

Can we create Mongoose model without schema?

You can use Mongoose with the collections that have schema and the node driver or another mongo module for those schemaless ones.


1 Answers

Check this code example, it should work as You need.

I don's see here any non-understanding part.

Ask questions in comments, I can explain if don't understand.

var tokenSchema = mongoose.Schema({
  owner: { 
    type: 'String',
    required: true,
    index: {
      unique: true
    }
  }, 
  token: {
    type: ['String'],
    default: []
  }
});

var Token = module.exports = mongoose.model('tokens', tokenSchema);

//save token, if token document exist so push it in token array and save
module.exports.saveToken = function(owner_id, token, callback){
    Token
      .findOne({owner: owner_id})
      .exec(function(err, tokenDocument) {
        if(tokenDocument) {
          if(tokenDocument.token.indexOf(token) > -1) { // found that token already exist in document token array
            return callback(null, tokenDocument); // don't do anything and return to callback existing tokenDocument
          }

          tokenDocument.token.push(token);
          tokenDocument.save(callback);
          return; // don't go down, cuz we already have a token document
        }

        new Token({owner: owner_id, token: [token]}).save(callback); // create new token document with single token in token array
    });
}

//get all tokens by owner_id
module.exports.getAllTokens = function(owner_id, callback){
    Token
      .findOne({owner: owner_id})
      .exec(function(err, tokenDocument) {
        callback(err, tokenDocument.token);
      });
}
like image 179
num8er Avatar answered Sep 21 '22 12:09

num8er