我正在尝试按照这些说明来查询模型的属性数据库。在本地开发服务器上测试时一切正常,但是当我将其部署到 firebase 时出现此错误:
Uncaught (in promise) {msg: "function 'userFunction' was not found."}
下面是我的代码:
const viewerHelperPlugin = {
install(Vue){
Vue.prototype.$viewerHelper = {
getElementsByPropertyValue: function (viewer, propertyName, propertyValue, operator) {
return new Promise(function(resolve, reject) {
function userFunction (pdb, userData) {
var attrIndex = -1;
// Iterate over all attributes and find the index to the one we are interested in
pdb.enumAttributes(function(i, attrDef, attrRaw){
var name = attrDef.name;
if (name === userData.propertyName) {
attrIndex = i;
return true; // to stop iterating over the remaining attributes.
}
});
// Early return is the model doesn't contain data.
if (attrIndex === -1)
return [];
var returnElements = [];
pdb.enumObjects(function(dbId) {
// For each part, iterate over their properties.
pdb.enumObjectProperties(dbId, function(attrId, valId){
if (attrId === attrIndex) {
var value = pdb.getAttrValue(attrId, valId);
if (userData.operator === "==" && value === userData.propertyValue) {
returnElements.push(dbId);
}
else if (userData.operator === ">" && value > userData.propertyValue) {
returnElements.push(dbId);
}
else if (userData.operator === ">=" && value >= userData.propertyValue) {
returnElements.push(dbId);
}
else if (userData.operator === "<" && value < userData.propertyValue) {
returnElements.push(dbId);
}
else if (userData.operator === "<=" && value <= userData.propertyValue) {
returnElements.push(dbId);
}
else if (userData.operator === "!=" && value != userData.propertyValue) {
returnElements.push(dbId);
}
}
});
});
return returnElements;
}
var thePromise = viewer.model.getPropertyDb().executeUserFunction( userFunction,
{propertyName: propertyName, propertyValue: propertyValue, operator: operator } );
thePromise.then(function(retValue){
resolve(retValue);
});
});
}
}
} }
我不确定为什么它找不到该函数,因为我在语句上方立即声明了它。
我相信我已经明白了。我使用 Vue 作为前端,当我将函数从 Vue 实例中拉出并放入全局 JavaScript 作用域时,它就起作用了。可能与“userFunction”名称要求有关? Vue 可能会在构建过程中将函数重命名为其自己的格式。
如果您在使用 Quasar 框架时遇到函数重命名问题,这里是对我有用的解决方案:
确保您已将 terser-webpack-plugin 作为开发依赖项安装在项目中:
npm 安装 terser-webpack-plugin --save-dev
2.更新 quasar.conf.js 以在文件开头导入 TerserPlugin:
// quasar.conf.js
const TerserPlugin = require('terser-webpack-plugin'); // Add this line to import TerserPlugin
module.exports = function (/* ctx */) {
return {
build: {
// Configure Webpack optimization
extendWebpack(cfg) {
if (process.env.NODE_ENV === 'production') {
// Create a new TerserPlugin instance with the desired options
const newTerserPlugin = new TerserPlugin({
terserOptions: {
keep_fnames: true,
},
});
// Replace the existing TerserPlugin instance with the new one
cfg.optimization.minimizer = cfg.optimization.minimizer.map((minimizer) =>
minimizer.constructor.name === 'TerserPlugin' ? newTerserPlugin : minimizer
);
}
},
},
// ... other config options ...
};
};