我有一个关于江湖骗子回复装饰的问题。我无法在响应代码块中调用另一个 js 文件中的函数。有人可以给我提示吗?
我的js文件:utils.js
function getRandomCharAndNum(min, max){
let returnStr = "";
const range = (max ? Math.round(Math.random() * (max-min)) + min : min);
const charStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for(let i=0; i<range; i++){
const index = Math.round(Math.random() * (charStr.length-1));
returnStr = returnStr + charStr.substring(index,index+1);
}
return returnStr;
}
江湖骗子中的响应代码是
const utils = require('./utils/utils');
responses: [{
is: {
statusCode: 200,
headers: {
"Consent-Type": "application/json"
}
},
_behaviors: {
decorate: `(config) => {
config.response.body = {
id: utils.getRandomCharAndNum()
};
}`
}
}]
错误是: ojit_代码
最佳答案
您必须将 getRandomCharAndNum 函数内联到装饰函数,或者直接将其包含在装饰函数中。看起来您有一些调用 mountebank 的包装器(未显示),但您不能在装饰器之外包含相关的辅助函数。
关于javascript - 如何从mountebank响应 block 中的另一个js文件调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58873500/