swift - 在 ubuntu 上使用 MongoSwift 时,Vapor 崩溃
问题描述
我试图为我的应用程序创建一个蒸汽 API。在我的本地机器(Mac OS)上,我所有的代码和我的 API 都运行良好,但是当我将它上传到我的 ubuntu 服务器时,它崩溃了......
我已经做了以下代码:
vapor clean
vapor update
vapor build
这些命令都没有返回任何类型的错误,但是当我运行 vapor 时,会发生以下情况:
root@rt-server:/usr/local/uprising/server# vapor run
Running server ...
root@rt-server:/usr/local/uprising/server#
搜索了一下并尝试解决我的问题,我发现如果我评论以下代码( configure.swift ),我可以让蒸汽运行:
// Register Mongo Client
let client = try! MongoClient()
let db = client.db("myDB")
let collection:MongoCollection<Document>
do {
collection = try db.createCollection("myCollection")
} catch {
collection = db.collection("myCollection")
}
// Create an unique index on timestamp
// because I expect only one daily
let indexOptions = IndexOptions(name: "timestamp", unique: true)
let model = IndexModel(keys: [ "timestamp": 1] , options: indexOptions)
do {
try collection.createIndex(model)
} catch {
print("Index already exist!")
}
services.register(client)
我有一些问题可能会帮助我弄清楚如何解决我的问题:
如何在 ubuntu 上使用命令行调试蒸汽?在我的mac中,当我使用xcode时很容易......
我可以测试我的 MongoDB C 驱动程序是否正确安装?
有谁知道我做错了什么?它在我的mac上完美运行......
对不起,我的英语不好...如果有人知道如何帮助我,我将不胜感激!
解决方案
我是 MongoSwift 的开发者之一。
你是怎么安装C驱动的?
崩溃可能来自您正在执行的第一行try! MongoClient()
,您可能还想尝试将其放入 try/catch 中。
也只是一个注释,您可以将您的集合代码简化为:
let collection = db.collection("myCollection")
createCollection
仅当您想创建具有非默认选项的集合时才需要使用。否则,集合将在您第一次使用时自动创建(在这种情况下,当您添加索引时)。
推荐阅读
- javascript - 使用 WHILE 函数从数组中打印出对象信息
- python - 分类特征的俱乐部标签的 Pythonic 方式
- php - php中同时多个请求
- javascript - 我怎样才能在柏树中做一个 if else ?
- linux - 无法在 x64 ubuntu linux 上添加自定义 hello 系统调用
- css - 我怎样才能只使用一个部分作为我的主页
- java - OFBiz 如何设置空默认值
在服务定义中 - jasper-reports - 如何对齐几个基于 JFreeChart 的条形图的 Y 轴?
- reactjs - Material UI:在组件外使用媒体查询
- python - 获取标题并发布它们(请求)PYTHON