云开发,本地调试只要开启就能成功调用云函数,关了调试,立刻失败报错?
报错内容:
errMsg: "Error: errCode: -404011 cloud function execution error | errMsg: cloud.callFunction:fail requestID 18b3e866-5eb7-11ea-b3ea-5254007aa7a1, cloud function service error code -504002, error message Unexpected token {; at cloud.callFunction api; "
代码片段:
index.js
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const common=require('./common');
const UserServiceImpl = require('./service/userServiceImpl').UserServiceImpl;
const userServiceImpl = new UserServiceImpl();
// 云函数入口函数
exports.main = async (event, context) => {
try{
if(event.url==='userServiceImpl.setUserInfo'){
return await userServiceImpl.setUserInfo(event.data)
}else{
return common.getCommonRes({}, "error", "未找到API:" + event.url)
}
}catch{
return common.getCommonRes({}, "error", "未知异常")
}
return event
}
common:
let app={
globalData:{
controllerLog:true
}
}
/**
* 下划线转换驼峰
* [@param](/user/param) {*} name
*/
const toHump = function (obj) {
try {
if (Object.prototype.toString.call(obj) === "[object Object]") {
let tempObj={};
for(let key in obj){
let upperKey=key.replace(/\_(\w)/g, function (all, letter) {
return letter.toUpperCase();
});
tempObj[upperKey]=obj[key];
}
return tempObj;
}else{
return obj
}
} catch{
return obj
}
}
const getCommonRes = function (data, type = "success", msg = "操作成功") {
let commonRes = {
code: null,
data: toHump(data),
msg: msg
}
try {
if (type === "success") {
commonRes.code = "200";
} else if (type === "error") {
commonRes.code = "500";
}
} catch{
commonRes.code = "10086";
}
return commonRes;
}
const checkPhoneReg = function (phone) {
try {
return (/^1[3456789]\d{9}$/.test(phone));
} catch{
return false
}
}
const printLog = function (type, msg) {
if (app.globalData[type]) {
try {
console.log(type + ">>>>>" + JSON.stringify(msg))
} catch{
console.log(type + ">>>>>" + msg)
}
}
}
module.exports = {
getCommonRes,
checkPhoneReg,
printLog
}
databaseCommon:
// 云函数入口文件
const cloud = require('wx-server-sdk')
const env = "dev-qyj77";
cloud.init();
const db = cloud.database();
/**
* 驼峰转换下划线
* 获取与数据库交互的entity
* [@param](/user/param) {*} name
*/
const toLine = function (obj) {
try {
if (Object.prototype.toString.call(obj)==="[object Object]") {
let tempObj={};
for(let key in obj){
let lowerKey=key.replace(/([A-Z])/g,"_$1").toLowerCase()
tempObj[lowerKey]=obj[key];
}
return tempObj;
} else {
return obj
}
} catch{
return obj
}
}
/**
* 从获取的对象中,抽出实体类的内容(LIST版本)
* [@param](/user/param) {*} entity
* [@param](/user/param) {*} obj
*/
const getCommonList = function (entity, list) {
let res = [];
try {
list.forEach(element => {
res.push(getCommonObj(entity, element));
});
} catch{
}
return res;
}
/**
* 从获取的对象中,抽出实体类的内容
* [@param](/user/param) {*} entity
* [@param](/user/param) {*} obj
*/
const getCommonObj = function (entity, obj) {
let resObj = entity;
try {
let tempEntity = Object.assign({}, entity, toLine(obj))
let resultEntity = {};
for (let key in entity) {
// if(obj[key]!==undefined){
resultEntity[key] = tempEntity[key];
// }
}
resObj = resultEntity;
} catch{
}
return resObj;
}
const getUserController = function () {
return db.collection("user");
}
const getOrderController = function () {
return db.collection("order");
}
module.exports = {
getUserController,
getCommonList,
getCommonObj,
getOrderController,
}
UserController:
const db = require("../databaseCommon");
const printLog = require("../common").printLog;
const entity = require("../entity/user.js").user;
const controller = db.getUserController();
const logType = "controllerLog";
class UserController {
constructor() {
}
/**
* 查询所有用户信息
* */
getAllUserInfo() {
return new Promise((success, error) => {
controller.get().then(res => {
let returnRes = db.getCommonList(entity, res.data)
success(returnRes)
})
})
}
/**
* 根据ID查询用户信息
*/
getUserInfoById(id) {
return new Promise((success, error) => {
controller.where({
_id: id
}).get().then(res => {
printLog(logType, "=====根据ID查询用户信息=====")
if (res.data.length > 1) {
printLog(logType, "=====根据ID查询用户信息,查询出多条信息!默认选择第一条=====")
printLog(logType, res.data)
}
let returnRes = db.getCommonObj(entity, res.data[0])
printLog(logType, returnRes)
success(returnRes)
})
});
}
/**
* 根据手机号查询用户信息
*/
getUserInfoByPhone(phone) {
return new Promise((success, error) => {
controller.where({
phone: phone
}).get().then(res => {
printLog(logType, "=====根据手机号查询用户信息=====")
if (res.data.length > 1) {
printLog(logType, "=====根据手机号查询用户信息,查询出多条信息!默认选择第一条=====")
printLog(logType, res.data)
}
let returnRes = db.getCommonObj(entity, res.data[0])
printLog(logType, returnRes)
success(returnRes)
});
})
}
/**
* 根据实体类插入用户信息
*/
setUserInfo(obj) {
return new Promise((success, error) => {
try {
let resultEntity = db.getCommonObj(entity, obj);
delete resultEntity._id;
controller.add({
data: resultEntity
}).then(res => {
printLog(logType, "=====插入用户信息:成功=====")
printLog(logType, res)
printLog(logType, resultEntity)
success()
}).catch(res => {
printLog(logType, res)
})
} catch (res) {
error(res)
}
})
}
/**
* 以ID为key,根据实体类更新用户信息
*/
updateUserInfo(obj) {
return new Promise((success, error) => {
try {
let { _id, ...resultEntity } = db.getCommonObj(entity, obj);
controller.doc(_id).update({
data: resultEntity
}).then(res => {
printLog(logType, "=====更新用户信息:成功=====")
printLog(logType, resultEntity)
success()
}).catch(res => {
printLog(logType, res)
})
} catch (res) {
error(res)
}
})
}
}
module.exports = {
UserController,
}
// export default UserController;
userServiceImpl:
const controller = require("../controller/userController").UserController;
const common = require("../common");
const userController = new controller();
class UserServiceImpl {
/**
* 获取所有用户信息
*/
getAllUserInfo() {
return new Promise((success, error) => {
userController.getAllUserInfo().then(res => {
success(common.getCommonRes(res))
})
})
}
/**
* 根据ID查询用户信息
*/
getUserInfoById(id) {
return new Promise((success, error) => {
userController.getUserInfoById(id).then(res => {
success(common.getCommonRes(res))
})
})
}
/**
* 根据手机号查询用户信息
*/
getUserInfoByPhone(phone) {
return new Promise((success, error) => {
userController.getUserInfoById(id).then(res => {
success(common.getCommonRes(res))
})
})
}
/**
* 根据实体类插入用户信息;
* 已手机号为key,无则新建,有则更新
*/
setUserInfo(obj) {
return new Promise((success, error) => {
// 判断手机号正确性
if (common.checkPhoneReg(obj.phone)) {
// 根据手机号查找用户信息
userController.getUserInfoByPhone(obj.phone).then(res => {
obj.updated = new Date();
if (res.phone) {
let postObj = Object.assign(res, obj);
userController.updateUserInfo(postObj).then(() => {
success(common.getCommonRes());
})
} else {
obj.created = new Date();
userController.setUserInfo(obj).then(res => {
success(common.getCommonRes());
})
}
})
} else {
success(common.getCommonRes({}, "error", "手机号码错误"))
}
})
}
}
module.exports = {
UserServiceImpl,
}
// export default UserServiceImpl;