Wednesday, 3 May 2017

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

I'm trying to post the data form , but I am getting the following error, When hit the submit :

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

My Post was successful as captured :

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

But the Data is not saved in the MongoDB. I was using MEAN Stack to post the data and I am using Angular2.

My 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);

});

And I posting the data as follows:

router.post('/movie',function(req,res){
        console.log("Under Post call....");
       var movieObj= req.body;
       var jsontest = JSON.stringify(movieObj);

        db.movies.save(jsontest,function(err,docs){
            if(err){
            res.send(err);
             }
             res.json(docs);
       });
});

My Target is to save data in the MongoDB.



via Prashant

No comments:

Post a Comment