如何在angularjs和mongoose中切换多个表(文档)(How to switch among multiple tables (documents) in angularjs and mongoose)
我的目标是让mongoDB具有多个表(文档集合),每个用户都有一个表。 那么如何在运行时在多个表之间“切换”(特定用户登录后是否知道特定表)? 我把这段代码剪掉了......
var mongoose = require('mongoose'), Schema = mongoose.Schema; var TicketSchema = new Schema({ Unit_descriptor: String, ... Total_tons: Number }, { strict: false }); module.exports = mongoose.model('tickets_user1', TicketSchema);
...我希望能够为当前登录的用户切换表格,例如:表格可以使用上面代码中的用户名后缀。
我试图从控制器和交换表创建稍后调用的导出函数,如下所示:
var auth = require('../../auth/auth.service.js'); ... module.exports.initTable = function initTable() { module.exports = mongoose.model('tickets_user2', TicketSchema); };
...没有交换表,user1的数据仍然存在。
那么,我怎么能实现这个目标呢? 谢谢。
My goal is to have mongoDB with multiple tables (collections of documents), each per some user. So how can I "switch" among multiple tables at runtime (particular table is known after particular user is logged in)? I have this code snipped...
var mongoose = require('mongoose'), Schema = mongoose.Schema; var TicketSchema = new Schema({ Unit_descriptor: String, ... Total_tons: Number }, { strict: false }); module.exports = mongoose.model('tickets_user1', TicketSchema);
...and I want to be able to switch table for currently logged user, for example: tables could have suffix with username as in code above.
I tried to create exported function called later from controller and swap table, like this:
var auth = require('../../auth/auth.service.js'); ... module.exports.initTable = function initTable() { module.exports = mongoose.model('tickets_user2', TicketSchema); };
...which does not swap tables, user1's data are still in place.
So, how could I achieve this? THX.
原文:https://stackoverflow.com/questions/32440606
最满意答案
你得到一个JSON字符串,所以你可以尝试
let jsonstring = "\"asdf\"" let data = jsonstring.data(using: .utf8) do { if let str = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as? String { print(str) } } catch let caught as NSError { }
You get a json string so you can try
let jsonstring = "\"asdf\"" let data = jsonstring.data(using: .utf8) do { if let str = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as? String { print(str) } } catch let caught as NSError { }
相关问答
更多-
按照你的观点,你将它转换为数据,然后试图将数据转换为JSON(失败,不是JSON)并将其转换为字符串,基本上你有一堆毫无意义的转换。 只要数组仅包含JSON可编码值(字符串,数字,字典,数组,零),您可以使用NSJSONSerialization来完成此操作。 取而代之的是做数组 - >数据 - >字符串部分: Swift 3/4 let array = [ "one", "two" ] func json(from object:Any) -> String? { guard let data ...
-
如何在swift中解析json(将json字符串转换为字符串)(How to parse json in swift (convert json string to string))[2022-05-14]
你得到一个JSON字符串,所以你可以尝试 let jsonstring = "\"asdf\"" let data = jsonstring.data(using: .utf8) do { if let str = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as? String { print(str) } } catch let caught as ... -
简单干净的方式将JSON字符串转换为Swift中的对象(Simple and clean way to convert JSON string to Object in Swift)[2022-11-17]
这里有一些技巧如何从简单的例子开始。 考虑你有以下的JSON数组字符串(类似于你的): var list:Array= [] // left only 2 fields for demo struct Business { var id : Int = 0 var name = "" } var jsonStringAsArray = "[\n" + "{\n" + "\"id\":72,\ ... -
与Codable复杂方式 用unkeyedContainer解码数组 使用带条件的while循环!isAtEnd并在do - catch块中解码Int 。 如果它解码失败String 使用传统JSONSerialization简单方法 将对象反序列[CustomStringConvertible]并使用"\($0)"将数组映射到[String] 编辑: 这是一个示例如何使用Decodable解码数组,如果项是成对的并且类型顺序相同: let jsonArray = """ ["A", 1, "A1", 13 ...
-
基于讨论 import Foundation let firstName = "Joe" let lastName = "Doe" let middleName = "Mc." let age = 100 let weight = 45 let jsonObject: [String: [String:Any]] = [ "user1": [ "first_name": firstName, "middle_name": middleName, " ...
-
这已经是一个JSON字符串,只需发送它附加标题。 如果您需要访问这些值,那么您需要使用swiftyJSON或JSONswift之类的东西。 This is already a JSON string just send it appended with the headers. If you need to access these values, then you need to use something like swiftyJSON or JSONswift.
-
字符串不是AnyObject类型。 对象是引用类型 ,但swift中的String具有值语义 。 然而, 字符串可以是Any类型,所以下面的代码可以工作。 我建议你阅读Swift中的引用类型和值语义类型; 它是一个微妙但重要的区别,它与大多数其他语言所期望的不同,其中String通常是引用类型(包括目标C)。 let jsonObject: [String: Any] = [ "firstname": "aaa", "lastname": "sss", ...
-
如果您的示例语法与您的真实JSON相同,则JSONLint表示您需要双重引用名称和值。 仅在这种情况下,请使用以下替换调用: var jsontemp = yourjson.replace((/([\w]+)(:)/g), "\"$1\"$2"); var correctjson = jsontemp.replace((/'/g), "\""); //yourjson = "{one: 'one', two: 'two'}" //jsontemp = "{"one": 'one', "two": 'two' ...
-
如果仔细观察jsonObject(with:options:)返回的内容,您将看到它是[String: Any]或[Any] ,具体取决于您的JSON。 因此, jsonString实际上存储了一个[String: Any] ,甚至认为编译器认为它是Any类型: let jsonString = try? JSONSerialization.jsonObject(with: data!, options: []) print(jsonString!) 如果你试图将它传递给convertToDicti ...
-
将没有数组或字典的JSON反序列化为顶级对象时,可以传递.allowFragments选项: let jsonString = "\"3\"" let jsonData = jsonString.data(using: .utf8)! let json = try! JSONSerialization.jsonObject(with: jsonData, options: .allowFragments) if let str = json as? String { print(str) // ...