1

I need to store responses to questions in Mongo. I am using Mongoose. My query looks like this right now:

router.post('/answers', expressJwt({secret: config.secret}), function (req, res, next) {
    var user = req.user
    var question = req.body.question
    var answer = req.body.answers
    var id = question._id

    db.User.update({email: user.email}, {$set: {answers[question._id]: answer}}, function (err, doc) {
        if (err) {
            console.error('problem updating answers object', err)
            return res.json(err)
        }
        console.log(doc)
        return res.json('successfully updated answers')
    })
})

I get an error 'unexpected token [' How can I add properties to my answers object?

I had to make a db call to get the answers object, then modify it, then update it back to the db. Here is the code I used. Note: use lean and exec with mongoose to get raw object otherwise you will run into problems modifying mongoose objects.

router.post('/answers', expressJwt({secret: config.secret}), function (req, res, next) {
    var user = req.user
    var question = req.body.question
    var answer = req.body.answers
    var id = question._id

    db.User.findOne({email: user.email}).lean().exec(function (err, user) {
        user.answers[question._id] = answer
        db.User.update({email: user.email}, {$set: {answers: user.answers}}, function (err, doc) {
            if (err) {
            console.error('problem updating answers object', err)
            return res.json(err)
        }
            console.log(doc)
            return res.json('successfully updated answers')
        })
    })
})
3
  • What does console.dir(answers[question._id]) output? Commented Sep 5, 2015 at 4:24
  • @mscdex Well, it depends on the context, but nothing in this case. I am trying to add a new property to the answers object which is the id of the question being answered : answer array. If you retrieved the user from the db and accessed this answers object, it would depend on whether or not they answered that specific question. Commented Sep 5, 2015 at 4:27
  • just looked up console.dir() the properties would be id : answers, id : answers for all questions. Commented Sep 5, 2015 at 4:34

2 Answers 2

1

First,you have to set id in your answer object before database call,then replace your field with your new field

router.post('/answers', expressJwt({secret: config.secret}), function (req, res, next) {
    var user = req.user
    var question = req.body.question
    var answer = req.body.answers

    answer.question._id = question._id;

    db.User.update({email: user.email}, {$set: {answers: answer}}, function (err, doc) {
        if (err) {
            console.error('problem updating answers object', err)
            return res.json(err)
        }
        console.log(doc)
        return res.json('successfully updated answers')
    })
})
Sign up to request clarification or add additional context in comments.

1 Comment

yea, this is what I did. it's similar to what you wrote, but with an additional db call to get the answers object. I will accept your answer and edit my question with the code.
1

Store it in a variable instead of using the literal object syntax:

var $set = {};
$set[answers[question._id]] = answer;
db.User.update({email: user.email}, {$set: $set}, function (err, doc) {
// ...

Also, if you have ES6 features available to you (e.g. a recent version of io.js) you can use the special bracket notation in object literals to achieve the same thing:

db.User.update({email: user.email}, {$set: {[answers[question._id]]: answer}}, function (err, doc) {
// ...

3 Comments

The $set method gives me a 500 internal server error and the second method continues to give me 'unexpected token [' and crash the server
i have also tried findOneAndUpdate with upsert: true as mentioned in other SO posts, but no luck. I have updated my post with the full route I'm working on.
I must not have ES6 features because the 500 error was coming from the $set[answers...] = answers line.

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.