i***@163.com
i***@163.com
  • 发布:2020-04-09 16:02
  • 更新:2020-10-25 20:58
  • 阅读:823

使用阿里云空间,调用add()写不进数据库,为啥?

分类:uniCloud
exports.main = async (event, context) => {  
  const db = uniCloud.database();  
  const dbCmd = db.command;  

  var phone = JSON.parse(event.body).tel.toString();  

  var res = await db.collection('user').where({  
    tel: phone  
  }).get();  
  if(res.data[0]){  
    return {status: 3,msg: "该手机号码已被注册"}  
  }  
  if (!phone) {  
    return {status: 2,msg: "手机号码不能为空"}  
  }  
  if (phone.length !== 11){  
    return {status: 1,msg: "请输入11位手机号码"}  
  }  

  // console.log(tel);  
  db.collection('user').add({  
    tel: phone,  
    userName: phone  
  })  
  return {status: 0,msg: "注册成功"}  
}  

上面这段代码写不进数据库
但是当我将那些if语句和res变量全部注释掉,就可以写进数据库了,这是为啥?是我写法有问题么?但是注释和没注释,接口都有输出注册成功的啊?下面代码:

exports.main = async (event, context) => {  
  const db = uniCloud.database();  
  const dbCmd = db.command;  

  var phone = JSON.parse(event.body).tel.toString();  

  // var res = await db.collection('user').where({  
  //   tel: phone  
  // }).get();  
  // if(res.data[0]){  
  //   return {status: 3,msg: "该手机号码已被注册"}  
  // }  
  // if (!phone) {  
  //   return {status: 2,msg: "手机号码不能为空"}  
  // }  
  // if (phone.length !== 11){  
  //   return {status: 1,msg: "请输入11位手机号码"}  
  // }  

  // console.log(tel);  
  db.collection('user').add({  
    tel: phone,  
    userName: phone  
  })  
  return {status: 0,msg: "注册成功"}  
}
2020-04-09 16:02 负责人:无 分享
已邀请:
i***@163.com

i***@163.com (作者)

该问题已解决

DCloud_uniCloud_WYQ

DCloud_uniCloud_WYQ

注意await

  • i***@163.com (作者)

    恩,当时忘记写await了

    2020-04-16 14:31

4***@qq.com

4***@qq.com - 暖水袋

在哪写await啊?

该问题目前已经被锁定, 无法添加新回复