efficient_work
efficient_work
  • 发布:2022-12-24 10:05
  • 更新:2022-12-25 11:16
  • 阅读:285

【报Bug】schema扩展ext.js引起的Unexpected token .

分类:uniCloud

产品分类: uniCloud/App

示例代码:

下面是查询代码

<template>  
<unicloud-db  
            ref="udb"  
            :collection="collectionList"  
            :where="where"  
            page-data="replace"  
            :orderby="orderby"  
            :getcount="true"  
            :page-size="options.pageSize"  
            :page-current="options.pageCurrent"  
            v-slot:default="{ data, pagination, loading, error, options }"  
            :options="options"  
        >  
            <view v-if="error">{{ error.message }}</view>  
            <view v-else>{{ data }}</view>  
        </unicloud-db>  
</template>  
<script>  

const db = uniCloud.database()  
const dbOrderBy = 'created_time desc' // 排序字段  
export default {  
    data() {  
        return {  
            collectionList: 'order',  
            adminVersion: version,  
            where: '',  
            orderby: dbOrderBy  
    }  
    },  
}  
</script>  

下面是schema

{  
    "bsonType": "object",  
    "required": ["create_uid", "created_time", "product_id", "date", "uid"],  
    "permission": {  
        "read": true,  
        "create": "auth.uid!=null",  
        "update": "auth.uid==doc.uid",  
        "delete": "auth.uid==doc.uid"  
    },  
    "properties": {  
        "_id": {  
            "description": "ID,系统自动生成"  
        },  
        "created_time": {  
            "bsonType": "timestamp",  
            "title": "创建时间",  
            "description": "创建时间",  
            "forceDefaultValue": {  
                "$env": "now"  
            }  
        },  
        "create_uid": {  
            "bsonType": "string",  
            "title": "创建者",  
            "forceDefaultValue": {  
                "$env": "uid"  
            }  
        },  
        "date": {  
            "bsonType": "timestamp",  
            "title": "订货日期"  
        },  
        "uid": {  
            "bsonType": "string",  
            "title": "用户",  
            "defaultValue": {  
                "$env": "uid"  
            }  
        },  
        "store_id": {  
            "bsonType": "string",  
            "title": "门店",  
            "foreignKey": "store._id",  
            "enum": {  
                "collection": "store",  
                "field": "name as text,_id as value"  
            },  
            "componentForEdit": {  
                "name": "uni-data-checkbox"  
            }  
        },  
        "state": {  
            "bsonType": "int",  
            "title": "状态",  
            "enum": [{  
                "text": "待发货",  
                "value": 0  
            }, {  
                "text": "待确认",  
                "value": 1  
            }, {  
                "text": "已完成",  
                "value": 2  
            }],  
            "defaultValue":0  
        },  
        "product_id": {  
            "bsonType": "array",  
            "title": "商品"  
        },  
        "trade_snapshot": {  
            "bsonType": "string",  
            "title": "交易快照"  
        }  
    }  
}  

操作步骤:

使用上面代码部署到线上运行

预期结果:

正常返回数据

实际结果:

报错:“Unexpected token .”

bug描述:

我的项目有多个表,本地运行没问题,然后发布上线,其中有两个表查询报错“Unexpected token .”看截图,这两个表刚好用了schema.ext.js,但是本地调试都没问题
然后我把线上的ext.js删掉,仍然报错
以为是代码问题,然后我用下面最简单的代码放在首页查询数据表,依然报错“Unexpected token .”
数据表、扩展和云函数都上传更新了

2022-12-24 10:05 负责人:无 分享
已邀请:
efficient_work

efficient_work (作者)

更新:基本可以确定是schema扩展引起的问题,当在控制台删除schema扩展后,需要重新上传schema表,就可以恢复正常了,如果这个时候又重新上传ext.js,又会报错
附上我的扩展代码,不过我监听的是update,实际执行的是查询操作,也是报错

module.exports = {  
    trigger: {  
        // 注册数据表的read前事件  
        beforeUpdate: async function(  
            // 确定要监听的什么样的JQL指令  
            {  
                collection,  
                operation,  
                where,  
                updateData,  
                field  
            } = {}) {  
            // 当上述jql指令被触发时,将执行这里的代码。这里就是普通的uniCloud代码,可以调用uniCloud的各种api。  
            console.log("这个触发器被触发了")  
        },  
        afterUpdate: async function({  
            collection,  
            operation,  
            where,  
            updateData,  
            clientInfo  
        } = {}) {  
            if (JSON.stringify(updateData) === JSON.stringify({  
                    state: 1  
                })) {  
                const _id = where._id  
                const db = uniCloud.database()  
                const dbCmd = db.command  
                const getRes = await db.collection('order').doc(_id).get()  
                const {  
                    product_id  
                } = getRes?.data?. [0]  
                // 更新库存  
                let task = []  
                for (let i = 0; i < product_id.length; i++) {  
                    const item = product_id[i]  
                    const p = new Promise(resolve => {  
                        const updateProductData = {  
                            stock: dbCmd.inc(-item.num)  
                        }  
                        db.collection('product')  
                            .doc(item._id)  
                            .update(updateProductData)  
                            .then(res => {  
                                resolve(res?.updated)  
                            })  
                            .catch(err => {})  
                    })  
                    task.push(p)  
                }  
                Promise.all(task).then(res => {  
                    console.log('resres', res);  

                })  
            }  

        }  
    }  
}  
DCloud_uniCloud_WYQ

DCloud_uniCloud_WYQ

?.可选链操作符是不支持的语法,另外你说的删除云端schema扩展之后多久测试依然报错

  • efficient_work (作者)

    确实,去掉?.就正常了,为什么本地运行就没问题

    删除云端schema扩展,我是马上测试,还是报错,但是我重新上传数据表的schema.json就可以了

    2022-12-26 11:47

  • DCloud_uniCloud_WYQ

    回复 efficient_work: 本地使用的是HBuilderX带的nodejs。版本是16,云端现在最高只能选择12

    2022-12-26 16:22

  • zttUnicloud

    该问题依然存在,同样添加了 ***.schema.ext.js 报这个错误,

    Error: Unexpected token {

    at vendor.js?t=wechat&s=1675940300774&v=d527ecf88965c8286d018789b2b29d7c:3(env: Windows,mp,1.06.2301040; lib: 2.28.1)

    2023-02-09 21:09

要回复问题请先登录注册