首页 > 解决方案 > 在 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)

我有一些问题可能会帮助我弄清楚如何解决我的问题:

对不起,我的英语不好...如果有人知道如何帮助我,我将不胜感激!

标签: swiftmongodbubuntuvapor

解决方案


我是 MongoSwift 的开发者之一。

你是怎么安装C驱动的?

崩溃可能来自您正在执行的第一行try! MongoClient(),您可能还想尝试将其放入 try/catch 中。

也只是一个注释,您可以将您的集合代码简化为:

let collection = db.collection("myCollection")

createCollection仅当您想创建具有非默认选项的集合时才需要使用。否则,集合将在您第一次使用时自动创建(在这种情况下,当您添加索引时)。


推荐阅读