Ich versuche, Formulardaten zu schreiben, aber ich bin den folgenden Fehler immer auf einreichen: Kann nicht Eigentum ‚_id‘ auf Zeichenfolge ‚{‚‘‚[object Object]‘}‘: createTypeerror:
TypeError: Cannot create property '_id' on string '{"[object Object]":""}'.
wurde mein Beitrag erfolgreich eingefangen:
Movie {id: "8", firstname: "test", lastname: "test", hero: "test", photo: "xxxxxxxx"}
Aber die Daten werden in MongoDB nicht gespeichert. Ich benutze den MEAN Stack mit Angular2.
Mein 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);
});
poste ich die Daten wie folgt:
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);
});
});
Der Ausgang was i bin immer { "_id": "590aad3ec7133419f056bc77", "\ "{\\" [Objekt Objekt] \\ ": \\" \\ ", \\" _ ID \\ ": \\" 590aa9c455f16d0eb440673e \\ "} \" ":" "}]
Beim Treffen die http://localhost:3000/api/movies URL.
Hinweis: Ziel ist es, Daten in MongoDB zu speichern.
ich diese Option versucht, aber das gleiche bekommen. Sobald ich die URL treffe: http: // localhost: 3000/api/filme bekomme ich {"_id": "590aa9c455f16d0eb440673e", "[object Object]": ""}]. – Prashant
@ Prashant dann sendet das Front-End eine solche Antwort –
Von Front-End, sobald ich auf Senden klicken. Ich bekomme: Film {id: "3", Vorname: "cc", Nachname: "zxc", Held: "xcz", Foto: "xzc"}, was korrekt ist. – Prashant