正在获取查询查找的属性

Getting properties of Query Find

本文关键字:属性 查找 查询 获取      更新时间:2023-09-26

在我的ember应用程序的登录功能原型中,我使用路由根据给定的用户名查询商店。如果找不到该用户名,我的API将返回一个带有消息属性的对象。这是一条路线:

App.LoginRoute = Ember.Route.extend({
    actions: {
        getUsername: function(username){
            this.store.find('user', {username: username}).then(function(user){
            var eid = user.get('eid');
            console.log(eid);
            if (eid) {
                self.controller.send('transition', "index");
                }
            else {
                self.controller.set('model', "Oops! That's not right.");}
                });
            }
    }
});`

如果数据库中存在用户名,API将发送回用户对象。如果用户名存在,它会加载到商店中,我可以在Ember Inspector中的Data下看到记录。但是,我不知道如何获取该用户对象的属性。

.then中,我传递返回的信息,并尝试调用该信息的.get,但始终返回未定义的信息。

获取store.find('store', {query})返回的属性的正确方法是什么?

find-by-query返回一个集合。

this.store.find('user', {username: username}).then(function(userCollection){
  // this would be the user if it existed
  var user = userCollection.get('firstObject');
  self.controller.send('transition', "index");
});     

当用户不存在时,您可能应该返回404错误代码,而不是有效的响应,然后点击promise的失败部分。

this.store.find('user', {username: username}).then(function(userCollection){
  // this would be the user if it existed
  var user = userCollection.get('firstObject');
  self.controller.send('transition', "index");
}, function(){
  // failure happened
  self.controller.set('model', "Oops! That's not right.");}
});