【发布时间】:2019-12-01 12:15:32
【问题描述】:
我的目标是更新存储在 MongoDB 中的值,因此我决定使用 mongoose 来查看数据并对其进行编辑,但是;它给了我一个错误。 也许我的方法不对,有没有人已经实现了这种工作。
import * as React from "react";
import * as mongoose from 'mongoose'
export interface State {
}
export default class mongodb extends React.Component<State> {
constructor(props: {}) {
super(props);
}
private setupDb() : void {
var mongoDb = 'mongodb://localhost/My_db';
mongoose.connect(mongoDb);
console.info("Connected to Mongo.")
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB Connection error'));
}
componentDidMount(){
this.setupDb()
}
render() {
return (<div></div> );
}
}
错误:
TypeError: mongoose__WEBPACK_IMPORTED_MODULE_1__.connect 不是 功能
【问题讨论】:
-
不建议直接从 react 连接到你的数据库。前端的用户可以使用代码和数据库链接。安全问题。您应该使用后端来处理来自 react 的调用。
-
是的,我知道,我只是在测试,首先,需要先从前端更改,然后再更改后端。有什么方法可以编辑吗?以及为什么会出现此错误。
标签: database reactjs mongodb typescript mongoose