node.js - 类型错误 : Cannot create property '_id' on string '{"[object Object ]":""}'

标签 node.js mongodb express mean-stack angular2-services

我正在尝试发布表单数据,但在提交时收到以下错误:

TypeError: Cannot create property '_id' on string '{"[object Object]":""}'.

我的帖子已成功捕获:

Movie {id: "8", firstname: "test", lastname: "test", hero: "test", photo: "xxxxxxxx"}

但是数据没有保存在MongoDB中。我将 MEAN 堆栈与 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","\"{\\"[object Object]\\":\\"\\",\\"_id\\":\\"590aa9c455f16d0eb440673e\\"}\"":""}]

当点击http://localhost:3000/api/movies时网址。

注意: 目的是将数据保存在MongoDB中。

最佳答案

当您传递字符串时,MongoDB 接受对象

错误

var jsontest = JSON.stringify(movieObj); 

正确

var jsontest=movieObj;

P.S:假设 req.body 是正确的电影对象。

关于node.js - 类型错误 : Cannot create property '_id' on string '{"[object Object ]":""}',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43764172/

相关文章:

node.js - 如何使用 Mongoose 将动态字段添加到现有集合

ios - 使用 MongoDB 和 Eve 快速开发 iOS 原型(prototype)?

node.js - Mongoose:remove() 对已删除的项目返回 true

Javascript/Node.js 数组内存管理

javascript - 如何使用更多 "expect"函数扩展 frisby.js?

sql - 如何检索用户输入的特定合作伙伴 ID 的电子邮件并将其发送回 Controller 。

node.js - 这是服务器代码还是应用程序代码的问题?

javascript - 更改聚合结果

html - Mongoose/NodeJS - 从数据库检索字符串但认为未定义

angularjs - 使用express渲染jade模板后 Bootstrap 按钮间距丢失