我有一台运行ExpressJs的服务器,这是我的功能:
var db = require('./db');
app.get('/findPlace', (request, response) => {
var placeName = request.query.name;
db.FindPlace(placeName).then(function(items){
console.info('The promose was fullfilled with items!', items);
response.json(items);
}, function(err){
console.error('The promise was rejected',err,err.stack);
});
});
db.js在哪里
module.exports = {
FindPlace: function(placeName)
{
return mongodb.connect(uri, { useNewUrlParser: true }).then(function(client){
var db = client.db('social-capital');
var places = db.collection("Places");
return places.find({"name":{ $eq: placeName}}).toArray();
}).then(function(items){
return items;
});
}
};
我正在尝试将JSON数组返回到我的iOS应用程序,但是由于iOS调用没有等待实现诺言,它一直返回nil。这是我发出请求的iOS方法:
guard var checkedInURL = URLComponents(string: Constants.BASE_URL + Constants.FIND_PLACES) else { return }
let queryTiems = URLQueryItem(name: "name", value: placeName)
checkedInURL.queryItems = [queryTiems]
checkedInURL.percentEncodedQuery = checkedInURL.percentEncodedQuery?.replacingOccurrences(of: "+", with: "%2B")
guard let finalUrl = checkedInURL.url else { return }
var request = URLRequest(url: finalUrl)
request.httpMethod = "GET"
URLSession.shared.dataTask(with: request) { (data, response, error) -> Void in
print("IN METHOD")
guard let data = data, // is there data
let response = response as? HTTPURLResponse, // is there HTTP response
(200 ..< 300) ~= response.statusCode, // is statusCode 2XX
error == nil else { // was there no error, otherwise ...
return
}
let responseObject = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any]
print(responseObject)
}.resume()