loopbackjs - 在 beforeRemote('create' 上存储特定请求的变量并在 afterRemote('create' 上使用它
问题描述
我想在 beforeRemote('create' 上存储特定请求的变量并在 afterRemote('create' 上使用它。我知道我可以将变量存储在模型类变量中,但我担心它将在请求之间共享。
例如:
MyModel.beforeRemote('create', function(ctx, unused, next) {
// Here i want to store a variable like:
this.storedValue = "xxx";
}
然后,在这里使用它:
MyModel.afterRemote('create', function(ctx, unused, next) {
console.log(this.storedValue);
}
我将在模型中声明的 this.storedValue 是静态模型还是实例模型?
解决方案
尝试这个:
MyModel.beforeRemote('create', function(ctx, unused, next) {
// Here i want to store a variable like:
ctx.hookState.storedValue = "xxx";
}
MyModel.afterRemote('create', function(ctx, unused, next) {
const storedValue = ctx.hookState && ctx.hookState.storedValue;
console.log(storedValue);
}
推荐阅读
- c# - 如何先显示文本,直到从数据库 sql 检索图像
- mongodb - 使用 mongoDB 中的聚合查询以获取特定的子文档?
- css - 如何将两个 flexbox 容器并排设置
- ibeacon - 使用 Android 信标库进行传输时,无法在 Google Beacon Tools 应用程序的未注册选项卡中看到 Eddystone UID
- rdf - RDF 可以对带有边缘属性的标记属性图进行建模吗?
- javascript - querySelectorAll 返回的节点问题
- javascript - ASP.NET onClientClick 返回 false 不起作用
- react-native - 将 base64 图像 uri 发送到 ImageManipulator
- nativescript - 如何在 Nativescript 中为 TabViewItem 赋予宽度?
- java - 如何获取任意整数数组并返回具有相同整数但没有重复的数组