7

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);
}
3
  • 1
    is Token defined in this file ? How is it passed to it Commented Jun 8, 2016 at 8:47
  • @MukeshSharma updated my question Commented Jun 8, 2016 at 8:56
  • please add tokenSchema definition, and type exact problem. You're saying: Token.insert is not a function. We - help You fix it, but after I get that You need to insert token into token array. So please fix Your question. Commented Jun 8, 2016 at 9:41

3 Answers 3

3

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);
      });
}
Sign up to request clarification or add additional context in comments.

12 Comments

this won't work, forgot to mention my schema the token is an array. Like this var tokenSchema = mongoose.Schema({ "owner" : { type:String }, "token" : [] });
You've not mentioned about schema anywhere. Check out new changes in my answer
The issue is it will still create another set of insert if the owner is the same. How to do if the owner is the same just push the token, not duplicate the entire document?
checkout "better solution", it gets Token by owner if it exists and pushes new token and saves it. otherwise creates new record with single token in it.
tq so much for ur help number!
|
1

You can use:

let newvalue = new Token({"owner":owner_id,"token":token});
newvalue.save();

2 Comments

save versus insert verus create what is the different?
it's dumb that Mongoose does not have an insert API, because that's the native API, I guess Model.create() takes the place of Model.insert().
1

Here i defined what is the problem that why they did not find the insertMany

generally, we create an instance to save a document in DB like this :

const instance = new Model(data);

After then we hit the save query

instance.save(data).then().catch()

But when we talk about the insertMany Section, No need to create any instance. you can insert all entries directly via Model

eg.

Modal.insertMany(data).then().catch()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.