TypeError:无法在字符串"{""[object Object]":“""}"上创建属性"_id" [英] TypeError: Cannot create property '_id' on string '{"[object Object]":""}'
问题描述
我正在尝试发布表单数据,但是在提交时遇到以下错误:
I'm trying to post form data, but I'm getting the following error on submit:
TypeError:无法在字符串'{"[object Object]":"}''上创建属性'_id'.
TypeError: Cannot create property '_id' on string '{"[object Object]":""}'.
我的信息被成功捕获:
电影{id:"8",名字:"test",姓氏:"test",英雄:"test",照片:"xxxxxxxx"}
Movie {id: "8", firstname: "test", lastname: "test", hero: "test", photo: "xxxxxxxx"}
但是数据未保存在MongoDB中.我在Angular2中使用MEAN堆栈.
But the data is not saved in MongoDB. I'm using the MEAN stack with Angular2.
我的 server.js :
//Need to write the Express code
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var indexApi = require('./routes/index');
var movieApi = require('./routes/movie');
var port = 3000;
var app = express();
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// parse application/vnd.api+json as json
//app.use(bodyParser.json({ type: 'application/json' }))
app.use('/',indexApi);
// Register the movie.js file
app.use('/api',movieApi);
// View Engine
app.set('views',path.join(__dirname, 'views'));
app.set('view engine','ejs');
app.engine('html',require('ejs').renderFile);
// Set static folder so that our all the things we read from client folder
app.use(express.static(path.join(__dirname,'client')));
app.listen(port,function(){
console.log("Server started at port number : "+ port);
});
我将数据发布如下:
my app.component.ts:
saveDetails(movieObj){
//Note: I am getting the movie object here
this.moviesService.saveMovie(movieObj).
subscribe((data=> this.movieObj = JSON.stringify(data._body);
console.log(this.movieObj);
);
}
and movies.service.ts:
saveMovie(movie){
//Note: I am getting the movie object here also
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
return this.http.post('http://localhost:3000/api/movie',
movie, {
headers: headers});
}
movie.js:
router.post('/movie',function(req,res){
console.log("Under Post call....");
var movieObj= req.body;
// Note: here in the console everthing is comming as undefined thats is the problem
console.log("[_id]"+movieObj._id+"[id]"+movieObj.id+"[firstname]"+movieObj.firstname+"[lastname]"+movieObj.lastname+"[hero]"+movieObj.hero+"[photo]"+movieObj.photo);
db.movies.save(movieObj,function(err,docs){
if(err){
res.send(err);
}
res.json(docs);
});
});
我得到的输出是{"_id":"590aad3ec7133419f056bc77","\" {\\"[对象对象] \\":\\"\\",\\"_ id \\":\ \"590aa9c455f16d0eb440673e \\"} \":"}]
The output what i am getting is {"_id":"590aad3ec7133419f056bc77","\"{\\"[object Object]\\":\\"\\",\\"_id\\":\\"590aa9c455f16d0eb440673e\\"}\"":""}]
点击 http://localhost:3000/api/movies 网址时.
注意: 目的是将数据保存在MongoDB中.
Note: The aim is to save data in MongoDB.
推荐答案
当您传递String
时,MongoDB接受Object
.
MongoDB accepts an Object
whereas you are passing a String
.
错
var jsontest = JSON.stringify(movieObj);
正确
var jsontest=movieObj;
P.S:假设主体是正确的电影对象.
P.S : assuming req.body is correct movie object.
这篇关于TypeError:无法在字符串"{""[object Object]":“""}"上创建属性"_id"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!