Here's what my schema currently looks like (only the related section):
let UserAccSchema = new Schema({
meta : {
accessControl: {
authTokens:[{
    issuedOn: {
        type: Date,
        default: Date.now()
    },
    expiresOn: {
        type: Date,
        default: Date.now() + 1728000000 //Defaults to 20-days
    },
    lastUsage: {
        type: Date,
        default: Date.now()
    },
    authAgent: {
        type: String,
        default: "default"
    }
}]}}
});
I want to push a new object in "meta/accessControl/authTokens". My current approach is:
UserAccSchema.methods.generateAuthToken = function (authAgent, cb) {
    console.info("MongoUser | Auth | Attempting to generate auth token for user | " + this._id);
    this.update({
        $push: {
            "meta.accessControl.authTokens": {
                authAgent: authAgent
            }
        }
    }, {safe: true, new: true}, function (err, obj) {
        if (err) {
            console.error("MongoUser | Auth | Error occurred while saving auth-token information | " + err);
            cb(new AppError("Auth token cannot be generated. Please try again.", AppError.ErrorCode.INTERNAL_SERVER_ERROR));
        } else {
            console.info("MongoUser | Auth | Auth token for user was generated | " + JSON.stringify(obj));
            cb(null, obj);
        }
    });
};
the above code is doing the job, but the problem that I'm having is when pushing the new object, the new object doesn't gets returned in:
function(err,obj) {
}
Instead returns this:
{"n":1,"nModified":1,"ok":1}
What I want to know:
- Where Am I wrong?
 - Am I doing this the right way? Any other way to $push the obj?
 
Thank You
via AnkitNeo
No comments:
Post a Comment