简体   繁体   English

Vue:Async Apollo mixin function 打印一个值,但返回 undefined

[英]Vue: Async Apollo mixin function prints a value, but returns undefined

Async/return undefined queries are common on here but I've tried all permutations of answers and seem to be getting nowhere... Apologies if I couldn't find it.异步/返回未定义的查询在这里很常见,但我已经尝试了所有答案的排列,但似乎无处可寻……如果我找不到它,请道歉。

I have taken all Apollo mutations out of my main Vue code into a global mixin so I can reuse, however I'm clearly not getting the async/promise syntax correct for one of the queries.我已将所有 Apollo 突变从我的主要 Vue 代码中取出到一个全局 mixin 中,以便我可以重用,但是我显然没有让其中一个查询的 async/promise 语法正确。

This is the mixin function:这是混合 function:

async DBgetGroupName(id) {
      if (id == null) {
        return process.env.VUE_APP_DEFAULT_GROUP_NAME;
      } else {
        await this.$apollo
          .query({
            query: GET_GROUP,
            variables: {
              id: id,
            },
          })
          .then((data) => {
            let ret = data.data.groups[0].group_name;
            return new Promise((resolve, reject) => {
              if (ret !== undefined) {
                console.log("Return value is " + ret);
                resolve(ret);
              } else {
                reject("Error");
              }
            });

      })
    }}

And this is the calling watcher.这是呼叫观察者。

watch: {
     selected: async function() {
      let vars = {
        id: null,
        name: "",
      };

      vars.id = this.selected;
       this.DBgetGroupName(vars.id).then((data) => {
        console.log("Name is " + data);
      });
    },
  }

Other permutations I've tried are simply returning the 'ret' value without wrapping in a promise, and using我尝试过的其他排列只是简单地返回“ret”值而不用 promise 包装,并使用

let data = await this.DBgetGroupName(vars.id)

in the watcher instead of the then block.在观察者而不是 then 块中。 The result in the console is always控制台中的结果总是

Return value is Test Group [or whichever group I've selected, ie the correct value]
Name is undefined

What am I missing?我错过了什么? Why isn't the value coming through to the watcher?为什么值没有传递给观察者?

Thank you for any advice.谢谢你的任何建议。

It turns out that I'm dealing with nested promises.事实证明,我正在处理嵌套的 Promise。 What I didn't realize is that the Apollo query itself is a promise, and anything returned within that block is actually returning to that promise, rather than to any calling function.我没有意识到 Apollo 查询本身是一个 promise,并且该块中返回的任何内容实际上都返回到该 promise,而不是任何调用 function。

So所以

     async DBgetGroupName(id) {
      if (id == null) {
        return process.env.VUE_APP_DEFAULT_GROUP_NAME;
      } else {
        await this.$apollo. // this call returns a promise
          .query({
            query: GET_GROUP,
            variables: {
              id: id,
            },
          })
          .then((data) => {
            let ret = data.data.groups[0].group_name;
            console.log("Return value is "+ret)
            return ret // this returns to the above promise, not the overall function
          });
    }},

Doesn't return anything, hence undefined.不返回任何东西,因此未定义。

So therefore to get it to work I just needed to return the initial promise, which would then return the ultimate return value when it is resolved.因此,为了让它工作,我只需要返回初始的 promise,然后在解决时返回最终的返回值。

Consequently, the calling function can just use await, rather than the then block.因此,调用 function 可以只使用 await,而不是 then 块。

Final, correct code:最终,正确的代码:

     async DBgetGroupName(id) {
      if (id == null) {
        return process.env.VUE_APP_DEFAULT_GROUP_NAME;
      } else {
        return await this.$apollo  //return this promise
          .query({
            query: GET_GROUP,
            variables: {
              id: id,
            },
          })
          .then((data) => {
            let ret = data.data.groups[0].group_name;
            console.log("Return value is "+ret)
            return ret. //value provided to above promise for returning
          });
    }},

and calling watcher:并调用观察者:

     selected: async function() {
      let vars = {
        id: null,
        name: "",
      };
      vars.id = this.selected;
      let data = await this.DBgetGroupName(vars.id)
      console.log("Name is " + data);
    },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM