te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Uncaught TypeError: Cannot read property 'then' of undefined using Sweet Alert - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught TypeError: Cannot read property 'then' of undefined using Sweet Alert - Stack Overflow

programmeradmin3浏览0评论

I am using SweetAlert2 and I am getting the following error

Uncaught TypeError: Cannot read property 'then' of undefined

When I use the exact same code as suggested in SweetAlert page.

swal({
title: 'Are you sure?',
  text: "You won't be able to revert this!",
  type: 'warning',
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'Yes, delete it!',
  cancelButtonText: 'No, cancel!',
  confirmButtonClass: 'btn btn-success',
  cancelButtonClass: 'btn btn-danger',
  buttonsStyling: false,
  closeOnConfirm: false,
  closeOnCancel: false
}).then(function(isConfirm) {
  if (isConfirm === true) {
    swal(
      'Deleted!',
      'Your file has been deleted.',
      'success'
    );
  } else if (isConfirm === false) {
    swal(
      'Cancelled',
      'Your imaginary file is safe :)',
      'error'
    );
  } else {
    // Esc, close button or outside click
    // isConfirm is undefined
  }
})

I have the code at the end of the page. I am using it in a Ruby on Rails app. I have tried returning a value as suggested in other posts, but it does not fix the problem

I am using SweetAlert2 and I am getting the following error

Uncaught TypeError: Cannot read property 'then' of undefined

When I use the exact same code as suggested in SweetAlert page.

swal({
title: 'Are you sure?',
  text: "You won't be able to revert this!",
  type: 'warning',
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'Yes, delete it!',
  cancelButtonText: 'No, cancel!',
  confirmButtonClass: 'btn btn-success',
  cancelButtonClass: 'btn btn-danger',
  buttonsStyling: false,
  closeOnConfirm: false,
  closeOnCancel: false
}).then(function(isConfirm) {
  if (isConfirm === true) {
    swal(
      'Deleted!',
      'Your file has been deleted.',
      'success'
    );
  } else if (isConfirm === false) {
    swal(
      'Cancelled',
      'Your imaginary file is safe :)',
      'error'
    );
  } else {
    // Esc, close button or outside click
    // isConfirm is undefined
  }
})

I have the code at the end of the page. I am using it in a Ruby on Rails app. I have tried returning a value as suggested in other posts, but it does not fix the problem

Share Improve this question asked Apr 14, 2016 at 0:01 marimafmarimaf 5,4103 gold badges52 silver badges70 bronze badges 6
  • Did you added reference to the related js file? – Hirad Nikoo Commented Apr 14, 2016 at 0:04
  • Because it means that there is not swal in the context. So it means either that the javascript file is not referenced in the file or the swal is not created properly. – Hirad Nikoo Commented Apr 14, 2016 at 0:05
  • @HiradNikoo I have swal referrenced. It actually pops up the modal view. But after the confirm/cancel it doesn't go through. – marimaf Commented Apr 14, 2016 at 0:49
  • Yeah it is definitely problem with Promise that OJay mentioned but please try my solution see if it works. – Hirad Nikoo Commented Apr 14, 2016 at 0:53
  • Sorry I think OJay's answer is correct. Please download Promise from here github./stefanpenner/es6-promise – Hirad Nikoo Commented Apr 14, 2016 at 1:01
 |  Show 1 more ment

1 Answer 1

Reset to default 11

looks as thought the sweetalert2 implementation uses the native promise api, and there is no included polyfill. Meaning might not work in some browsers. Even the github page shows the same error. Works fine when I try it in chrome, but does not work in IE11. I would look at including a promise API polyfill, depending on what browser/s you are trying to support. This has nothing to do with rails.

Polyfill is here or google ES6 promise polyfill. Include it before the sweetalert2 js file and you should be fine.

If don't understand what I am talking about with the promise API, good article here about it, or google JS promise.

Now if all of this is too hard, and/or confusing, why not just use the original sweetalert (link here) as it doesn't use the promise api. Not exactly sure why they forked, as I can't see any drastically new functionality, apart from using promises of course, and a couple of new modal icons (info and question)

发布评论

评论列表(0)

  1. 暂无评论