我想查询模型,然后根据结果做其他事情
问题:
exists = false
App.User.where(email: "example@domain.com").exists (error, exists) =>
console.log exists #=> true
console.log exists #=> false
示例为什么我想要这个:
class App.User extends Tower.Model
newUser: ->
exists = false
App.User.where(email: "some@email.com").exists (error, exists) =>
unless exists # here is always: exists = false
@.create
email: "some@email.com" # so user is created
我也尝试过这样的事情:
class App.User extends Tower.Model
newUser: ->
@.where(email: "some@email.com").exists (error, exists) =>
unless exists # statement works
@.create # but I can't create an user
email: "some@email.com"
App.User.create # also doesn't work
email: "some@email.com"
user = new App.User # this also fails
user.set "email", "some@email.com"
user.save()