【发布时间】:2020-11-02 08:19:21
【问题描述】:
我正在尝试使用 Angular 8 中的 mongoose.createConnection 连接到 mongodb,但我一直遇到此错误:
mongoose.createConnection is not a function
这是我的代码:
import { Component, OnInit } from '@angular/core';
import { RouteSegments } from '../mm-routing.module';
@Component({
selector: 'elastic-search',
templateUrl: './elastic-search.component.html',
styleUrls: ['./elastic-search.component.scss']
})
export class ElasticSearchComponent implements OnInit {
constructor() { }
ngOnInit() {
}
open() {
debugger;
const mongoose = require('mongoose');
const connection = mongoose.createConnection('mongodb://Test/Test', { useNewUrlParser: true });
var bookSchema = new mongoose.Schema({
title: String,
author: String,
description: String
});
var Book = mongoose.model("Book", bookSchema);
}
}
我也尝试过使用 mongoose.connection,但我在控制台中遇到了同样的错误。
【问题讨论】:
-
你能告诉我们控制台输出什么错误。
-
mongoose.createConnection 不是一个函数,总是这个。
-
@RobNone,根据mongoose code example,
createConnection的调用似乎是正确的。尽管您设法摆脱了console错误,但我们无法直接从 Angular 代码或任何客户端代码直接连接到mongodb。
标签: angular mongodb typescript mongoose frontend