sssaini
sssaini

Reputation: 405

Pushing Array of Objects Mongoose NodeJS

I have a mongoose schema defined as such,

var userSchema = mongoose.Schema({
    token: String,
    school: [{name: String, grade: String}]
});

As you can see it is "school" with two values of strings inside it. I am trying to push in values like this,

app.post('/array', function(req, res) {
    user.find({
        token: "c30a2402dcd8e4580cef8177516e8c57149425bf961a254fd79cf7b7280a6af0adf620d210c8b66989fe7fd35a3adc2756ec2089b415d27c1a1c097e88bc4666"
    }, function(err, users) {
        if (users.length != 0) {
            user.findOne({
                token: "c30a2402dcd8e4580cef8177516e8c57149425bf961a254fd79cf7b7280a6af0adf620d210c8b66989fe7fd35a3adc2756ec2089b415d27c1a1c097e88bc4666"
            }, function(err, doc) {
                doc.ar.push("sss", "A");
                doc.save();
            });
        } else {}
    });
});

But the push does not work and gives me an error that I cannot get rid of.

doc.ar.push({"sss","A"});
                   ^

SyntaxError: Unexpected token ,
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:387:25)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Module.require (module.js:367:17)
    at require (internal/module.js:16:19)
    at Object.<anonymous> (C:\..\app.js:27:1)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at startup (node.js:146:18)
    at node.js:404:3

Is there a way to easily push values in?

Upvotes: 1

Views: 83

Answers (1)

svens
svens

Reputation: 11628

That's a simple JavaScript syntax error. You probably want this:

doc.ar.push({name: "sss", grade: "A"});

Upvotes: 1

Related Questions