getModel()未定义 - 但已定义

时间:2017-06-05 05:24:49

标签: javascript node.js google-cloud-storage

我正在云存储上创建文件上传功能。这是后端代码:

const Storage = require('@google-cloud/storage')
const storage = Storage({
    projectId: 'a-1485'
})

const bucket = storage.bucket('a-1485.appspot.com')
function getPublicUrl (filename) {
  return 'https://storage.googleapis.com/a-1485.appspot.com/${filename}'
}

function sendUploadToGCS (req, res, next) {
  if (!req.file) {
    return next()
  }

  const gcsname = Date.now() + req.file.originalname
  console.log(gcsname)
  const file = bucket.file(gcsname)

  const stream = file.createWriteStream({
    metadata: {
      contentType: req.file.mimetype
    }
  })

  stream.on('error', (err) => {
    req.file.cloudStorageError = err
    next(err)
  })

  stream.on('finish', () => {
    req.file.cloudStorageObject = gcsname
    req.file.cloudStoragePublicUrl = getPublicUrl(gcsname)
    next()
  })

  stream.end(req.file.buffer);
}

module.exports = {
  getPublicUrl,
  sendUploadToGCS
}

在我的app.js文件中:

app.post('/upload-image', multer.single('image'), images.sendUploadToGCS, (req, res, next) => {
  let data = req.body
  console.log(data)
  if (req.file && req.file.cloudStoragePublicUrl) {
    data.imageUrl = req.file.cloudStoragePublicUrl
  }

  getModel().create(data, (err, savedData) => {
    if (err) {
      next(err)
      return
    }
    res.redirect(`${req.baseUrl}/${savedData.id}`);
  })
}
)

但是,当我上传图片时,我会收到一条错误,说“getModel()'没有定义。但正如您在上面所看到的,已定义。有什么问题?

0 个答案:

没有答案