Mongoose使用promise设置函数

时间:2023-02-06 02:33:09

Hello i am trying to set string value to my column of model. The string value comes with promise but it can't be returned.

您好我正在尝试将字符串值设置为我的模型列。字符串值带有promise,但不能返回。

    photoUrl: {
    type: String,
    set: setImage,
    default: ""
},

this is my model column

这是我的模型专栏

function setImage(base64) {
    uploadImage('users', base64).then(
        (publishUri) => {
            return publishUri
        }, 
        (err) => {
            return ''
        }
    )
}

this is my set function

这是我的设定功能

module.exports = {
    uploadImage: function(storageURI, base64) {
        return new Promise((res,rej) => {
            const storagePathURI = keys.devStorageURI + storageURI + '/';
            const uid = uuidv4();
            createFolder(storagePathURI, uid).then((fileUri, err) => {
                if (err) return rej(err)
                saveImage(fileUri, base64, storageURI, uid).then((publishStorageUri, err) => {
                    console.log(publishStorageUri)
                    console.log(err)
                    if (err) return rej(err)
                    return res(publishStorageUri)
                })
            })  
        })
    }
}

function createFolder(storagePathURI, uuid) {
    return new Promise((res,rej) => {
        mkdirp(storagePathURI, (err) => {
            if (err) return rej(err)
            const fileUri = storagePathURI + uuid + '.png';
            return res(fileUri);
        })
    })
}

function saveImage(fileUri, base64, storageURI, uuid) {
    return new Promise((res, rej) => {
        var base64Data = base64.replace(/^data:image\/png;base64,/, "");  
            fs.writeFile(fileUri, base64Data, 'base64', (err) => {
                if (err) return rej(err)
                return keys.prodPublishStorageURI + storageURI + '/'  + uuid + '.png';
            })
    })
}

and this is my storage helper class

这是我的存储助手类

class is working great and value return correctly but just my problem is i cant set the value that returned to my photoUrl column how can i solve this issue

class工作得很好,值正确返回但只是我的问题是我不能设置返回到我的photoUrl列的值如何才能解决这个问题

1 个解决方案

#1


0  

You are confusing promise with callback. I made some changes to the code:

你对回调的承诺感到困惑。我对代码做了一些更改:

module.exports = {
  uploadImage: function (storageURI, base64) {
      const storagePathURI = keys.devStorageURI + storageURI + '/';
      const uid = uuidv4();
      //return a promise, createFolder returns a promise
      return createFolder(storagePathURI, uid)
      .then(
        fileUri =>
          //return result of saveImage (is a promise)
          saveImage(fileUri, base64, storageURI, uid)
      ).then(
        //in a promise chain you can return a value or promise of value
        //  if you return a value the next then is called with that value
        //  if you return a promise of value (like returning result of saveImage)
        //  the next then is called with the result of that promise
        publishStorageUri => {
          console.log("publish storage uri:",publishStorageUri);
          return publishStorageUri;
        }
      ).catch(
        err=>{
          console.warn("something went wrong:",err);
          //let code calling uploadImage know something went wrong
          return Promise.reject(err);
        }
      )
  }
}

const createFolder = (storagePathURI, uuid) =>
  new Promise(
    (res, rej) =>
      mkdirp(
        storagePathURI,
        (err) =>
          (err) 
            ? rej(err)
            : storagePathURI + uuid + '.png'
    )
  );

const saveImage = (fileUri, base64, storageURI, uuid) =>
  new Promise(
    (res, rej) =>
      fs.writeFile(
        fileUri,
        base64.replace(/^data:image\/png;base64,/, ""),
        'base64',
        (err) =>
          (err)
            ? rej(err)
            : keys.prodPublishStorageURI + storageURI + '/' + uuid + '.png'
      )
    );

Depending how badly you screwed up mkdirp this may or may not work.

根据你搞砸mkdirp的严重程度,这可能会也可能不会起作用。

update

更新

There are no asynchronous setters or getters in Mongoose schema, you can add an instance method on the schema:

Mongoose模式中没有异步设置器或getter,您可以在模式上添加实例方法:

yourSchema.methods.setPhotoUrl = function () {
  return setImage()
  .then(
    publishUri=>
      this.setPhotoUrl=publishUri
  ).then(
    ()=>this//return this
  )
}

#1


0  

You are confusing promise with callback. I made some changes to the code:

你对回调的承诺感到困惑。我对代码做了一些更改:

module.exports = {
  uploadImage: function (storageURI, base64) {
      const storagePathURI = keys.devStorageURI + storageURI + '/';
      const uid = uuidv4();
      //return a promise, createFolder returns a promise
      return createFolder(storagePathURI, uid)
      .then(
        fileUri =>
          //return result of saveImage (is a promise)
          saveImage(fileUri, base64, storageURI, uid)
      ).then(
        //in a promise chain you can return a value or promise of value
        //  if you return a value the next then is called with that value
        //  if you return a promise of value (like returning result of saveImage)
        //  the next then is called with the result of that promise
        publishStorageUri => {
          console.log("publish storage uri:",publishStorageUri);
          return publishStorageUri;
        }
      ).catch(
        err=>{
          console.warn("something went wrong:",err);
          //let code calling uploadImage know something went wrong
          return Promise.reject(err);
        }
      )
  }
}

const createFolder = (storagePathURI, uuid) =>
  new Promise(
    (res, rej) =>
      mkdirp(
        storagePathURI,
        (err) =>
          (err) 
            ? rej(err)
            : storagePathURI + uuid + '.png'
    )
  );

const saveImage = (fileUri, base64, storageURI, uuid) =>
  new Promise(
    (res, rej) =>
      fs.writeFile(
        fileUri,
        base64.replace(/^data:image\/png;base64,/, ""),
        'base64',
        (err) =>
          (err)
            ? rej(err)
            : keys.prodPublishStorageURI + storageURI + '/' + uuid + '.png'
      )
    );

Depending how badly you screwed up mkdirp this may or may not work.

根据你搞砸mkdirp的严重程度,这可能会也可能不会起作用。

update

更新

There are no asynchronous setters or getters in Mongoose schema, you can add an instance method on the schema:

Mongoose模式中没有异步设置器或getter,您可以在模式上添加实例方法:

yourSchema.methods.setPhotoUrl = function () {
  return setImage()
  .then(
    publishUri=>
      this.setPhotoUrl=publishUri
  ).then(
    ()=>this//return this
  )
}