无法分配给只读属性'_id'

时间:2016-04-23 09:02:51

标签: node.js mongodb

get error使用节点js在mongodb中插入数据时无法分配给只读属性'_id'

无法分配给

的只读属性'_id'
app.post('/post', function (req, res) {

var diagnosis;

var obj = req.headers['data'];

var temp = JSON.parse(obj)
console.log("Received body data:");
console.log(obj);

MongoClient.connect(url, function (err, db) {
    assert.equal(null, err);
    insertDocument(temp, db, function () {
        db.close();
    });
});


var insertDocument = function (obj, db, callback) {
    try {            
        console.log(obj);
        db.collection('diagnosis').remove();
        db.collection('diagnosis').insertOne(obj, function (err, result) {
            assert.equal(err, null);
            console.log("Inserted a document into the Diagnosis collection.");
            callback();
        });
        res.send("Inserted a document into the Diagnosis collection.");
    } catch (err) {
        console.log(err);
    }
};

请帮我解决这个错误

我的代码是

$scope.post = function () {

                var diagnosis = $scope.Treatments;


            var httpRequest = $http({
                method: 'POST',
                url: '/post',
                headers: {
                    'Content-Type': 'application/json; charset=utf-8',
                    'data': JSON.stringify($scope.Treatments)
                },

            }).success(function (data, status) {                    
            });
        };

})

我调用API的函数是

            {

                Diagnosis: '',
                Treatment: [
                    {
                        name: 'treatment1',
                        SubTreatment: [
                            {
                                name: 'subtreatment1',
                                SubTreatment2: [{ name: 'subtreatmentexp1' }]
                            }
                        ],
                    }
                ]
            };

治疗阵列......

$ scope.Treatments =

   "Diagnosis": "sss",

                    "Treatment": [

                        {

                            "name": "treatment1",
                            "SubTreatment": [
                                {
                                    "name": "subtreatment1",
                                    "SubTreatment2": [
                                        {
                                            "name": "subtreatmentexp1",
                                            "$$hashKey": "object:27",
                                            "Name": "sss"
                                        }
                                    ],
                                    "$$hashKey": "object:22",
                                    "Name": "sss"
                                }
                            ],
                            "$$hashKey": "object:12",
                            "Name": "sss"
                        }
                    ]
                }

生成的JSON以插入数据

{

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.textView.text = @"https://github.com/";

    [OGDataProvider sharedInstance].updateInterval = [NSNumber days:10];

    self.embeddedView = [[URLEmbeddedView alloc] initWithUrl:@""];
    [self.containerView addLayoutSubview:self.embeddedView andConstraints:@[
        self.embeddedView.Top,
        self.embeddedView.Right,
        self.embeddedView.Left,
        self.embeddedView.Bottom
    ]];
}

0 个答案:

没有答案