我跟随this tutorial设置了一个简单的Angular 6 / ExpressJS / mongoDb应用程序。我不得不在ExpressJS方面进行一些更改,因为我想在开发环境中运行Angular。
我的问题是,当我从我的应用程序中保存一本书时,#34;某些东西"确实发生了。将新记录插入mongoDb。这是控制台日志:
{
author: "4"
description: "4",
isbn: "4",
published_year: "4",
publisher: "4",
title: "4",
}
从formSubmit函数:
onFormSubmit(form:NgForm) {
console.log(form);
this.api.postBook(form)
.subscribe(res => {
let id = res['_id'];
this.router.navigate(['/book-details', id]);
}, (err) => {
console.log(err);
});
}
这也传递给api服务中的postBook
函数。
因此看起来数据正确地插入到表单中并传递给ExpressJS。
当我运行curl -i -H "Accept: application/json" localhost:3000/api
时,我确实得到了结果:
[
{"_id":"5b0e4f1dc547e5164c08acab","updated_date":"2018-05-30T07:13:33.050Z","__v":0},
{"_id":"5b0e4fe74d3d0b15f44ed717","updated_date":"2018-05-30T07:16:55.392Z","__v":0},
{"_id":"5b0e52c0b0710433b0986864","updated_date":"2018-05-30T07:29:04.551Z","__v":0},
{"_id":"5b0e5356b0710433b0986865","updated_date":"2018-05-30T07:31:34.290Z","__v":0}
]
但正如您所看到的那样,前端没有数据存储在数据库中。
这是我的Book.js模型:
const express = require('express')
const app = express()
const mongoose = require('mongoose');
const apiRouter = require('./routes/book');
const logger = require('morgan');
mongoose.connect('mongodb://localhost/movieseat', { promiseLibrary: require('bluebird') })
.then(() => console.log('connection successful'))
.catch((err) => console.error(err));
app.use(logger('dev'));
app.use(express.json());
app.use('/api', apiRouter);
app.use(express.urlencoded({ extended: false }));
app.listen(3000, () => console.log('Example app listening on port 3000!'))
module.exports = app;
因此,看起来ExpressJS在收集/存储来自前端的数据时遇到了问题。关于为什么会这样或如何修复它的任何想法?