如何用云函数获得表单数据?
发布于 4 年前 作者 czhang 13032 次浏览 来自 官方Issues

如何解决获取表单数据时返回的object Promise?

我用微信小程序云函数制作了一个发邮件的功能 主函数是填写 名字 提交表单到云数据库 

我现在是想发送邮件的内容是

姓名为+“名字”+的车主拨打了维修厂的电话

可是一直返回  

姓名为[object Promise]的车主拨打了维修厂的电话

我把代码贴一下 希望大佬分析!狠狠的感恩您!

我的Q838618033

(云数据库都配好了 每次也都更新依赖)

云函数:

const cloud = require('wx-server-sdk')
cloud.init()
//引入发送邮件的类库
var nodemailer = require('nodemailer')
// 创建一个SMTP客户端配置
var config = {
  host: 'smtp.qq.com'//网易163邮箱 smtp.163.com
  port: 465//网易邮箱端口 25
  auth: {
    user: '[email protected]'//邮箱账号
  }
};
// 创建一个SMTP客户端对象
var transporter = nodemailer.createTransport(config);
// 云函数入口函数
exports.main = async(event, context) => {
  // 创建一个邮件对象
  var mail = {
    // 发件人
    from'来自小程序管理员 <[email protected]>',
    // 主题
    subject: '有一位车主拨打了维修店电话',
    // 收件人
    to: '[email protected]',
    // text:'姓名为曹博信的车主拨打了维修厂的电话'
    // 邮件内容,text或者html格式
    text:'姓名为'+cloud.database().collection('userName').get()+'的车主拨打了维修厂的电话'
  };

  let res = await transporter.sendMail(mail);
  return res;
}

主函数:

Page({
 
  /**
   * 页面的初始数据
   */
  data: {
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoadfunction (options{
  },

  sendEmail(){
    wx.cloud.callFunction({
      name:"sendEmail",
      success(res){
        console.log("发送成功",res)
      },
      fail(res){
        console.log("发送失败",res)
      }
    })
  },

  // pnum(){
  //   wx.cloud.callFunction({
  //     name:"getData"
  //   })
  //   .then(res =>{
  //     console.log('666666666666666666',res)
  //     this.setData({
  //       userName:res.result.userName
  //     })
  //   })
  //   .catch(res =>{
  //     console('444444444444444',res)
  //   })

  // },

  formSubmitfunction (e{
    wx.redirectTo({
      url'/pages/4s/4s',
    })
    console.log('form发生了submit事件,携带数据为:', e.detail.value)
    var data=e.detail.value
   {
        wx.showLoading({
          title'数据正在提交中......',
          mask:"true"
        })
  
        const db = wx.cloud.database()
        db.collection('test').add({
        data: data,
        successres => {
          // 在返回结果中会包含新创建的记录的 _id
          this.setData({
            counterId: res._id
          })
          wx.showToast({
            title'请您拨打电话',
          })
          wx.hideLoading()
         console.log('[数据库] [新增记录] 成功,记录 _id: ', res._id)
        },
        failerr => {
          wx.showToast({
            icon'none',
            title'新增记录失败'
          })
          console.error('[数据库] [新增记录] 失败:', err)
        }
      })
    }
  },

  wxccall:function(e){
    wx.redirectTo({
      url'/pages/wxc/wxc',
    })
    console.log('form发生了submit事件,携带数据为:', e.detail.value)
    var data=e.detail.value
   {
        wx.showLoading({
          title'数据正在提交中......',
          mask:"true"
        })
  
        const db = wx.cloud.database()
        db.collection('test').add({
        data: data,
        successres => {
          // 在返回结果中会包含新创建的记录的 _id
          this.setData({
            counterId: res._id
          })
          wx.showToast({
            title'请您拨打电话',
          })
          wx.hideLoading()
         console.log('[数据库] [新增记录] 成功,记录 _id: ', res._id)
        },
        failerr => {
          wx.showToast({
            icon'none',
            title'新增记录失败'
          })
          console.error('[数据库] [新增记录] 失败:', err)
        }
      })
    }
  },
    bindDateChangefunction (e{
      console.log('picker发送选择改变,携带值为', e.detail.value)
      this.setData({
        date: e.detail.value
      })
    },
    bindRegionChangefunction (e{
      console.log('picker发送选择改变,携带值为', e.detail.value)
      this.setData({
        region: e.detail.value
      })
    },

})



    姓名
    
      
        
      
    
  

  
    您的爱车坏了,您打算去_修车
    
      4S店(推荐)电话
      维修厂电话
    
  
  
  获得4S店电话
  获得维修厂电话

   
  
    
    
  

  


菜鸟上路三天 还是希望各位大佬各抒己见 多谢指教

5 回复

想完整回答,结果是图片,代码换成文字才有人帮你。

简单提示:xxx+await cloud.database()…get()…

collection(‘userName’), 你确定是一个集合叫userName的东西,.get()取出来可是数组对象

跟云函数都没关系

回到顶部