2014-05-22 5 views
0

.push 메서드를 사용하여 포함 된 구조의 필드를 포함 된 모델 문서로 푸시하려고합니다. http://mongoosejs.com/docs/2.7.x/docs/embedded-documents.html임베디드 모델에 임베디드 워드 프로세서를 내놓으시겠습니까?

콘솔 오류이 생각 : SyntaxError: Unexpected token .

포함 된 모델의 삽입에 대한 합치 할 때 bar.this : req.body.this,을 밀어 ..for

모델은 다음과 같이 :

var OneModel = new Schema({ 
    foo: String, 
    bar: { 
     this : String, 
     that : Number, 
    } 
}); 

var TwoModel = new Schema({ 
    foo: String, 
    bar: { 
     this : String, 
     that : Number, 
    }, 
    modelone: [OneModel] 
}); 

그리고 NodeJS API는 다음과 같이 표시합니다.

var ModelsOneTwo = require('./app/models/modelsonetwo'); 

router.route('/modeltwo/:modeltwo_id') 

    // update TwoModel with this _id 
    //(accessed at PUT http://localhost:4200/api/v1/modeltwo/:modeltwo_id) 
    .put(function(req, res) { 

     ModelsOneTwo.findById(req.params._id, function(err, modeltwo) { 

      if (err) 
       res.send(err); 

      // embedded document updating 
      // http://mongoosejs.com/docs/2.7.x/docs/embedded-documents.html 
      modeltwo.modelone.push({ 

       foo : req.body.foo,    
       bar.this : req.body.this, 
       bar.that : req.body.that 

      }); 

      // save the modeltwo, and check for errors 
      modeltwo.save(function(err) { 

       if (err) 
        res.send(err); 

      res.json({ message: req.params.modeltwo_id + ' ' + req.body.foo }); 

      }); 

     }); 

    }); 

답변

1

모델에 bar의 속성을 설정하려면, 당신은 그것을 위해 추가 Object을 만들어야합니다 :

var OneModel = new Schema({ 
    foo: String, 
    bar: { 
     this : String, 
     that : Number, 
    } 
}); 
+0

:

modeltwo.modelone.push({ foo : req.body.foo, bar : { this : req.body.this, that : req.body.that } }); 

이이 스키마에 정의 된 방법과 유사 완벽하게 이해합니다. 많은 감사 – StackThis

관련 문제