无效的Cloudinary配置

8

我刚接触Keystone,并试图部署一个简单的网站模板以熟悉这项技术,我已经下载了所有必要的模块,并创建了一个包含所有依赖项的keystone.js文件和package.json文件。然而,在我尝试在终端中运行keystone.js时,我收到以下错误信息:

 Error: Invalid Configuration

 CloudinaryImage fields (Gallery.heroImage) require the "cloudinary config" option to be set.

 See http://keystonejs.com/docs/configuration/#cloudinary for more information.

我已经在cloudinary上设置了一个账户,并使用npm install确保它已经安装到系统中,但显然找不到配置文件。我认为这个问题很简单,只需要将我的配置字段放到代码的正确位置即可,但我似乎找不到任何关于在哪里插入我的账户详细信息的说明。如果有任何帮助,将不胜感激,请告诉我是否遗漏了任何重要的代码。
keystone.js:
    require('dotenv').load();

    // Require keystone
   var keystone = require('keystone'),
handlebars = require('express3-handlebars');


    // Initialise Keystone with your project's configuration.
   // See http://keystonejs.com/guide/config for available options
   // and documentation.


   keystone.init({

'name': 'Tech Website',
'brand': 'Tech Website',

'less': 'public',
'static': 'public',
'favicon': 'public/favicon.ico',
'views': 'templates/views',
'view engine': 'hbs',

'custom engine': handlebars.create({
    layoutsDir: 'templates/views/layouts',
    partialsDir: 'templates/views/partials',
    defaultLayout: 'default',
    helpers: new require('./templates/views/helpers')(),
    extname: '.hbs'
}).engine,

'auto update': true,
'session': true,
'auth': true,
'user model': 'Yes',
'cookie secret': 'pUO>=q^~Z.h]~pO"k;:]dTcTb:6pT3Xyassxdk>9K]7J0MGqSWWr;$rs6lG<XLdB'

});

    // Load your project's Models

keystone.import('models');

    // Setup common locals for your templates. The following are required for the
   // bundled templates and layouts. Any runtime locals (that should be set uniquely
  // for each request) should be added to ./routes/middleware.js

keystone.set('locals', {
_: require('underscore'),
env: keystone.get('env'),
utils: keystone.utils,
editable: keystone.content.editable
    });

    // Load your project's Routes

    keystone.set('routes', require('./routes'));

    // Setup common locals for your emails. The following are required by Keystone's
    // default email templates, you may remove them if you're using your own.

    // Configure the navigation bar in Keystone's Admin UI

    keystone.set('nav', {
'posts': ['posts', 'post-categories'],
'galleries': 'galleries',
'enquiries': 'enquiries',
'yes': 'yes'
});

// Start Keystone to connect to your database and initialise the web server

.start();

package.json

{
 "name": "tech-website",
  "version": "0.0.0",
  "private": true,
  "dependencies": {
  "keystone": "~0.2.27",
  "async": "~0.9.0",
  "underscore": "~1.7.0",
  "moment": "~2.8.1",
  "express3-handlebars": "~0.5.0",
  "handlebars": "^2.0.0-alpha.2",
  "dotenv": "0.4.0"
  },
  "devDependencies": {
  "grunt": "~0.4.4",
  "grunt-express-server": "~0.4.17",
  "grunt-contrib-watch": "~0.6.1",
  "grunt-contrib-jshint": "~0.7.1",
  "jshint-stylish": "~0.1.3",
  "load-grunt-tasks": "~0.4.0",
  "grunt-node-inspector": "~0.1.5",
  "time-grunt": "~0.3.1",
  "grunt-concurrent": "~0.5.0",
  "grunt-nodemon": "~0.2.1",
  "open": "0.0.5"
},
  "engines": {
  "node": ">=0.10.22",
  "npm": ">=1.3.14"
},
"scripts": {
"start": "node keystone.js"
},
"main": "keystone.js"
}
3个回答

8

您可以通过多种方式在KeystoneJS应用程序中配置Cloudinary

其中一种选项是设置CLOUDINARY_URL环境变量。由于您正在使用dotenv,因此可以在.env文件中执行此操作。

CLOUDINARY_URL=cloudinary://api_key:api_secret@cloud_name

第二种选择是在Keystonejs应用程序中设置cloudinary config设置。
您可以在keystone.init()中完成此操作。
keystone.init({
    ...
    'cloudinary config': 'cloudinary://api_key:api_secret@cloud_name',
    ...
});

...或使用keystone.set()方法。

keystone.set('cloudinary config', 'cloudinary://api_key:api_secret@cloud_name' );

这些都在 KeystonsJS配置 页面详细说明,但现在已经不存在了。

我做了这个,但在keystone管理/画廊中显示“未找到画廊”。 - Anenth
我假设您使用了自己的 Cloudinary 账户。如果您从未创建/上传任何图库,则管理员应显示“未找到图库”。请创建一个图库并上传一些照片。 - JME
我创建了一个Gallery模型var Gallery = new keystone.List('Gallery', { autokey: { from: 'name', path: 'key', unique: true }, nodelete:true, nocreate:true });` `Gallery.add({ name: { type: String, required: true }, publishedDate: { type: Date, default: Date.now }, images: { type: Types.CloudinaryImages } });` `Gallery.register();` 这是我设置配置的方式 `keystone.set('cloudinary config', 'cloudinary://333779167276662:_8jbSi9FB3sWYrfimcl8VKh34rI@keystone-demo' );` `keystone.start();` - Anenth
对我来说看起来不错。您仍需要创建一个新的图库并上传照片(从管理面板),以便它们出现在管理面板中。 - JME
如何从Cloudinary创建新的图库? - Anenth
显示剩余3条评论

3

添加内容 -

对我而言实际有效的是将配置设置部分放在

keystone.init 部分。使用setter方法对我无效。

keystone.init({
    ...
    'cloudinary config': 'cloudinary://api_key:api_secret@cloud_name',
    ...
});

上面的代码是唯一没有问题的代码。

2
遇到了类似的问题,把设值函数(setters)移到初始化函数(init)之前就可以解决。 - Yousef
同样在这里成功地将setter移动到init之上。对我来说解决了问题。 - Michael Paccione

1
这是我在Heroku上部署的方法。在keystone.js中添加一个块以使用环境变量:
if (keystone.get('env') == 'production'){
    keystone.set('cloudinary config', process.env.CLOUDINARY_URL);
    keystone.set('cookie secret', process.env.COOKIE_SECRET);
    keystone.set('mandrill api key', process.env.MANDRILL_API_KEY);
}

然后通过命令行在您的heroku实例上设置环境变量:

$ heroku config:set MANDRILL_API_KEY=YOUR_API_KEY
$ heroku config:set CLOUDINARY_URL=cloudinary://api_key:api_secret@cloud_name
$ heroku config:set NODE_ENV=production
$ heroku config:set COOKIE_SECRET=YOUR_COOKIE_STRING

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接