loopback Find()“where”子句不返回预期结果

时间:2017-03-09 07:34:02

标签: javascript mongodb api loopback apiconnect

我正在使用loopback为我的应用程序提供api,我尝试更改某些数据的GET请求。

截至目前,查询会获取特定API的所有结果:

      People
            .find({ where : {'town' : 'name of a town'}})
            .$promise
            // Promise is fulfilled and people returned
            .then(function(results){
              $scope.people = results;
            })
            // Promise is rejected and error catched
            .catch(function(err){
              $scope.errors.PeopleFind = JSON.stringify(err.data.error.message
                                                            ? err.data.error.message
                                                            : err.data.error.errmsg
                                                      );
            });

我已尝试在where子句中添加单引号或执行.find({ where : { town : 'name of a town' }}之类的操作。无论我把引号放在哪里,结果总是整个包。我如何查询我感兴趣的结果?

提前致谢

3 个答案:

答案 0 :(得分:7)

我找到了答案,感谢一位同事,我将在这里写下答案

Twitter.sharedInstance().logIn(withMethods: [.webBased,.systemAccounts,.all]) {(session, error) -> Void in

        if (session != nil) {
            print("signed in as \(session?.userName)");

            let client = TWTRAPIClient(userID: session?.userName)
            client.loadUser(withID: (session?.userID)!, completion: { (user, error) in

                let twitterClient = TWTRAPIClient.withCurrentUser()
                let request = twitterClient.urlRequest(withMethod: "GET",
                                                url: "https://api.twitter.com/1.1/account/verify_credentials.json",
                                                parameters: ["include_email": "true", "skip_status": "true"],
                                                error: nil)


                twitterClient.sendTwitterRequest(request) { response, data, connectionError in

                    print(data!)

                    let s :String = String(data: data! as Data, encoding: String.Encoding.utf8)!
                    //
                    //            let json = try JSONSerialization.jsonObject(with: responseData as Data, options: JSONSerialization.ReadingOptions.mutableLeaves) as? [String:AnyObject]
                    //

                    Twitter.sharedInstance().sessionStore.logOutUserID((session?.userID)!)

                    if let data = s.data(using: String.Encoding.utf8) {
                        do {
                            let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String:Any]
                            print(json!)

                        } catch {
                            print("Something went wrong")
                        }
                    }
                }
            })

        } else {
   }

loopback框架的文档没有声明你需要应用一个过滤器对象来实际过滤结果,实际上你可以用他们写的这个例子检查文档:

file

在where子句对象之前,您需要编写包含该子句的过滤器对象,这应该可以解决查询问题。

答案 1 :(得分:1)

像这样使用sizeof( char )

like

并确保您已在json文件中添加People.find({where: {Town : like: {currentUserTown}}}, function(err, res){ //code goes here. })

strictObjectIDCoercion

希望这会有所帮助。

答案 2 :(得分:0)

这是常见的错误,请检查您各自的模型People.json配置文件,并制作town: {"type":"string"}