插入数据时怎样判断表中是否已存在相同名称?
发布于 4 年前 作者 jing26 10342 次浏览 来自 问答

我以为这样return可以停止程序向下运行,但是没起作用,有人说是异步 问题,但不懂怎么改,

请教一下怎么改才能判断表中有存在一样名字的就停下运行?谢谢 

create: function () {

    if (!this.data.titleshow{

      wx.showToast({

        title: '请填写标题~',

        icon: 'none'

      });

      return;

    }


        db.collection(kinds).where({

       title:this.data.titleshow

     }).get().then(

       res=>{

            if(res.data.length>0)

            {

              wx.showToast({

                title: '已存在',

              })

              return;

            }

       }

     )

    





    wx.showLoading({

      title: '正在创建……',

      mask: true

    });

2 回复

create: async function () {   

   const res= await db.collection("kinds").where({

       title:this.data.titleshow

     }).get()

  if(res.data.length>0)

  {

   wx.showModal({

     title:"确认提示",

     content:"已经存在,是要更改值吗?",

     success(res)

     {

       if(res.confirm)

       {

         db.collection("kinds").where({

           title:this.data.titleshow

          }).update({

            data:{

              title:this.data.titleshow

            }

          })

          return;

       }

       else if(res.cancel)

       {

         return;

       }

     }

   })

  }

///这是一条分隔线-----------------------------------------------    wx.showLoading({

      title: "正在创建…",

      mask: true

    });

为什么确认提示框出来一下,还没有点确认就执行下去????

回到顶部