- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 Next.js v6 构建一个应用程序,我想用本地 mongodb 数据库的结果填充页面。
理想情况下,我想做一些像 this example from the tutorial 这样简单的事情,但是不使用 api 获取,只需向本地主机上的 mongodb 发送查询即可。
Next.js 教程中的示例
import Layout from '../components/MyLayout.js'
import Link from 'next/link'
import fetch from 'isomorphic-unfetch'
const Index = (props) => (
<Layout>
<h1>Batman TV Shows</h1>
<ul>
{props.shows.map(({show}) => (
<li key={show.id}>
<Link as={`/p/${show.id}`} href={`/post?id=${show.id}`}>
<a>{show.name}</a>
</Link>
</li>
))}
</ul>
</Layout>
)
Index.getInitialProps = async function() {
const res = await fetch('https://api.tvmaze.com/search/shows?q=batman')
const data = await res.json()
console.log(`Show data fetched. Count: ${data.length}`)
return {
shows: data
}
}
export default Index
我的问题是尝试连接我的数据库并将其暴露给应用程序,以便我可以在名为 resources.js 的页面上执行类似的操作:
import Layout from '../components/Layout'
import Link from 'next/link'
const Resources = (props) => {
<Layout>
<h3>Resources</h3>
<ul style={listStyle}>
{props.resources.map(({resource}) => (
<li key={resource._id}>
<Link href={resource.link}>
<a>{resource.title}</a>
</Link>
</li>
))}
</ul>
</Layout>
};
Resources.getInitialProps = async function() {
// get query from mongo DB
// req is 'undefined' -- I want to see the db here
const list = await req.db.get('resources').find();
return list;
}
export default Resources;
但我不知道如何在可以从应用程序中的任何页面访问的函数中公开数据库。似乎旧的示例教程在主 index.js
文件中使用了类似 server.use((req, res, next) => { req.db = dbToExpose });
的内容,但这似乎不适用于 Next.js v. 6?
例如,this sample repo来自this tutorial最新版本的 Next.js 似乎不适用于我。当您尝试从页面访问它时,通过 req.db
暴露的数据库会显示为“未定义”。但也许我还遗漏了一些其他问题。
这是我在基本服务器文件中尝试公开数据库的尝试,以便我可以从 pages/
中的文件访问它:
const express = require('express')
const next = require('next')
const monk = require('monk')('localhost/myDatabase')
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
app.prepare()
.then(() => {
const server = express()
// This is not working to expose the db!
server.use((req, res, next) => {
req.db = monk
next()
});
server.get('*', (req, res) => {
return handle(req, res)
})
server.listen(3000, (err) => {
if (err) throw err
console.log('> Ready on http://localhost:3000')
})
})
.catch((ex) => {
console.error(ex.stack)
process.exit(1)
})
最佳答案
您不应暴露任何安全数据或在 Nextjs 页面中进行数据库连接。这篇文章为 connect to database with NextJs 指明了方向没有API
关于javascript - 将 mongodb 数据库连接公开到 Next.js 6 应用程序中的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51207609/
例如 Form1 frm1 = new Form1(); TextBox tb = new TextBox(); frm1.Controls.Add(tb); 现在我不能说 f
我有一个日期过滤器,我已经在我的 View 中公开了它。我想让界面更加用户友好并加强它的外观。我不想选择日期,而是从以下选项中进行选择。 最后一天 上周 去年 全部 然后,这将过滤日期字段。这可能吗?
如何向用户公开我的用户控件组件之一的 ActualWidth 属性? 我找到了很多关于如何通过创建新的依赖属性和绑定(bind)来公开普通属性的示例,但没有关于如何公开像 ActualWidth 这样
Github 最近推出了项目功能。 当项目处于 repo 级别时,如果 repo 本身是公开的,那么任何人都可以访问这些项目。 但是,组织级别的项目仅对组织成员可见。 例如,https://githu
我想要从我的网络服务器访问 JavaScript 文件。 以便任何人都可以在其网站中访问和引用它。 e-g 假设 abcxyzserver.com 是我的网络服务器。 www.abcxyzserv
尝试使用curl命令上传到blob存储 curl --upload-file --url "https://.blob.core.windows.net//" 但不断收到“HTTP/1.1 404
我正在尝试获取 Canvas 的上下文,显然我收到错误Uncaught TypeError: Cannot call method 'getContext' of null 显然我在它初始化之前就得到
我正在对设置 HA 集群的解决方案进行故障排除。虽然我知道应用程序执行故障转移和故障回复所需的端口,但不知何故 dockerized 解决方案不起作用。我怀疑有一些我还不知道的端口。 目前,我的 EX
我试图在能够使用 Helm 运行的k8集群中设置Prometheus。当我使用外部IP将Prometheus-Server作为LoadBalancer服务公开时,访问仪表板。 当我尝试将此服务配置为C
我知道关于这个主题也有类似的问题,但我不完全确定他们正在解决同样的问题。所以要明确的是... 我有一个现有的类库,其中包含用于类型、业务逻辑和数据访问的命名空间。逻辑和数据访问命名空间中的类是静态的,
尝试使用curl命令上传到blob存储 curl --upload-file --url "https://.blob.core.windows.net//" 但不断收到“HTTP/1.1 404
1.)执行以下命令生成一个随机数,用于后面的步骤 NUMBER=$[ ( $RANDOM % 1000 ) + 1 ] echo $NUMBER 注意:将句子 your random number 替
类似这样的问题有很多,但仍然无法得到我真正想要的,而且它们都有一些与我不同的地方,那就是:我有一个 UserControl: 在名为UCProject 的类库项目中单独构建; UCProject 项目
我有一个这样的基类: public class BaseModalCommand { protected object m_commandArgument; protected i
给定以下 JQuery 插件。是否可以将变量“元素”公开给插件外部的 javascript?如果是这样,这是怎么做到的?对于此插件外部的 javascript,访问“元素”的语法是什么? (funct
我有两个使用 jhipster 创建的微服务。 (ms1 和 ms2) 我使用 AuthorizedFeignClient 在两个微服务之间进行通信。 ms1 有一些 DTO 类,用作 REST AP
我正在使用错误跟踪软件来报告网络浏览器中发生的任何错误,但我的生产站点上的代码已缩小。因此,调试几乎是不可能的(变量名被更改等)。 我想将完整的源映射文件投入生产,以便我可以调试这些错误,但在这样做时
我在 Kotlin 公开库中可以找到的所有 Material 都假定该表具有一个主标识列,因此在大多数示例中显示的实体继承了 IntEntity 抽象类。例如: class UserLocation(
我有一个类 (Capsule),它有很多 protected 方法 (30+)。这个想法是允许开发人员扩展此类并在类 (ImADev) 中使用 protected 方法,但将其留给开发人员将它们公开为
Tomcat 日志位置是: /apache/apache-tomcat-8.0.15/logs 允许通过浏览器访问这些日志的标准方法是什么? 为此启用 Tomcat 目录列表标准吗? 最佳答案 我曾在
我是一名优秀的程序员,十分优秀!