URL 一直有效,直到我通过命令行将其设置为 process.env.MONGOLAB_URI
URL works until I set it to process.env.MONGOLAB_URI via command line
我一直在尝试使用 flickr 的 API,现在我正在尝试将应用程序部署到 Heroku。为了做到这一点,因为我正在使用 MongoDB,所以我试图通过 mLab 将它变成 运行。我可以很好地登录到 shell,当我将 URL 作为字符串使用到数据库时,我可以将整个内容发送到 运行,但是当我分配 [=39] 时突然=] 到 process.env.MONGOLAB_URI
与 SET MONGOLAB_URI="mongodb://dbuser:dbpassword@ds041506.mlab.com:41506/flick
r_image_search"
(我正在使用 Windows 10)在本地命令行中尝试使用它,它停止工作。 我得到的错误消息是 "Error: invalid schema, expected mongodb"。 即使我使用 console.log(url)
它返回 "mongodb://dbuser:dbpassword@ds041506.mlab.com:41506/flickr_image_search"
(我保证我在考虑到我从字面上复制并粘贴了我在命令行中使用的 URL ,命令行都是绝对正确的),正如我所期望的那样。我使用的 IDE 是 Visual Studio 代码,我没有使用 heroku mLab 附加组件到 运行 这个,而是我在网站本身上的数据库。我没有想法,可以使用一些帮助。这是我用于应用程序的每个相关代码文件:
app.js
var express = require('express');
var app = express();
var flickr = require('./flickr.js');
var mongo = require('mongodb').MongoClient;
var path = require('path');
var url = process.env.MONGOLAB_URI;
var today = new Date();
var day = today.getDate();
var month = today.getMonth()+1;
var year = today.getFullYear();
var mins = today.getMinutes();
var hr = today.getHours();
today = month + '/' + day + '/' + year + ' ' + hr + ':' + mins;
var obj;
var search;
app.get('/', function(req, res){
res.sendFile(path.join(__dirname + '/index.html'));
});
app.get('/latest', function(req, res){
mongo.connect(url, function(err, db){
if(err) throw err;
var theCollection = db.collection('flickr_image_search');
theCollection.find({}).toArray(function(err, docs){
if(err) throw err;
search = docs;
return docs;
});
db.close();
});
res.send(search);
});
app.get(['/search/:text/:offSet2', '/search/:text'], function(req, res){
var lookInIt = req.params.text;
var listLength = req.params.offSet2;
var searched = flickr.search(lookInIt, listLength || 10);
obj = {
query: lookInIt,
offSet: listLength,
date: today
};
mongo.connect(url, function(err, db){
if(err) throw err;
var imageSearch = db.collection('flickr_image_search');
imageSearch.insert(obj);
db.close();
});
res.send(searched);
});
var port = 3000 || process.env.PORT;
app.listen(port, process.env.IP, function(){
console.log('LISTENING');
});
flickr.js
var private = require('./private');
var Flickr = require("flickrapi"),
flickrOptions = {
api_key: private.key,
secret: private.secret
};
var obj;
function search (query, offSet) {
Flickr.tokenOnly(flickrOptions, function (err, flickr) {
if (err) throw err;
flickr.photos.search({
text: query,
page: 1,
per_page: offSet,
sort: "relevance"
}, function (err, result){
if(err) throw err;
result = result.photos.photo;
for(var key in result){
var picUrl = 'https://farm' + result[key]["farm"] + '.staticflickr.com/' + result[key]["server"] + '/' + result[key].id + '_' + result[key].secret + '.jpg'
result[key].picUrl = picUrl;
var userUrl = 'https://www.flickr.com/photos/' + result[key].owner + '/' + result[key].id;
result[key].userUrl = userUrl;
delete result[key]["ispublic"];
delete result[key]["isfriend"];
delete result[key]['isfamily'];
delete result[key]['id'];
delete result[key]['owner'];
delete result[key]['server'];
delete result[key]['farm'];
delete result[key]['secret'];
}
obj = result;
});
});
return obj;
}
module.exports.search = search;
index.html
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<title>flickr Image Search</title>
<div class='container'>
<h1 class='text-center'>Flickr Image Search</h1>
<h3 class='well'>To get a series of URLs for images, type 'search/whatever-you-want-to-search/the-amount-of-photos-you-want-returned' in the address bar after 'someWebsite.com/'.
If you want to see the most latest searches that were searched, instead of 'search/whatever-you-want-to-search/the-amount-of-photos-you-want-returned' type 'latest'.
When you find the URL that you want, just copy+paste!</h3>
<p class=lead>This application is purely functional, so chances are the queries won't return clean.</p>
<p>This is an application that searches through the photos of Flickr, using its API.</p>
</div>
Heroku 使用 MONGODB_URI
,但你有 MONGOLAB_URI
。
这可以解释为什么字符串有效但环境变量无效。
尝试在 shell 中输入:heroku config
找到 heroku 配置变量。我敢猜测它是 MONGODB_URI
而不是 MONGOLAB_URI
。
如果是这种情况,请重置您的环境和 heroku 配置变量以使用 MONGODB_URI
。
您在评论中说:"I'm not sure how to check it on Heroku, especially since the app doesn't want to run on there. Heroku has been giving me troubles when deploying applications in general."
要检查您的配置变量,您可以在 shell 中键入 heroku config
,或者您可以导航到您的应用仪表板。单击设置。单击显示配置变量。
当您在仪表板中时,您可以通过单击“资源”选项卡来配置 Heroku 提供的 mLab Add-on。然后在 Add-on 的搜索框中,输入 mLab MongoDB。
点击它。这将带您进入 mLab 的 GUI,您可以在其中查看和管理您的 collections。
如果您想知道服务器设置是什么样的,请参阅下面的代码。这将在适当的地方连接到您的本地和 mLab 数据库。
if(process.env.MONGODB_URI) {
mongoose.connect(process.env.MONGODB_URI);
}else {
mongoose.connect(db, function(err){ //db = 'mongodb://localhost/yourdb'
if(err){
console.log(err);
}else {
console.log('mongoose connection is successful on: ' + db);
}
});
}
在声明 process.env 变量之前安装 dotenv (https://www.npmjs.com/package/dotenv)、require('dotenv')、然后安装 dotenv.load() 对我很有帮助。现在我的 .env 文件中的内容被读取到以前没有的地方。谢谢
我一直在尝试使用 flickr 的 API,现在我正在尝试将应用程序部署到 Heroku。为了做到这一点,因为我正在使用 MongoDB,所以我试图通过 mLab 将它变成 运行。我可以很好地登录到 shell,当我将 URL 作为字符串使用到数据库时,我可以将整个内容发送到 运行,但是当我分配 [=39] 时突然=] 到 process.env.MONGOLAB_URI
与 SET MONGOLAB_URI="mongodb://dbuser:dbpassword@ds041506.mlab.com:41506/flick
r_image_search"
(我正在使用 Windows 10)在本地命令行中尝试使用它,它停止工作。 我得到的错误消息是 "Error: invalid schema, expected mongodb"。 即使我使用 console.log(url)
它返回 "mongodb://dbuser:dbpassword@ds041506.mlab.com:41506/flickr_image_search"
(我保证我在考虑到我从字面上复制并粘贴了我在命令行中使用的 URL ,命令行都是绝对正确的),正如我所期望的那样。我使用的 IDE 是 Visual Studio 代码,我没有使用 heroku mLab 附加组件到 运行 这个,而是我在网站本身上的数据库。我没有想法,可以使用一些帮助。这是我用于应用程序的每个相关代码文件:
app.js
var express = require('express');
var app = express();
var flickr = require('./flickr.js');
var mongo = require('mongodb').MongoClient;
var path = require('path');
var url = process.env.MONGOLAB_URI;
var today = new Date();
var day = today.getDate();
var month = today.getMonth()+1;
var year = today.getFullYear();
var mins = today.getMinutes();
var hr = today.getHours();
today = month + '/' + day + '/' + year + ' ' + hr + ':' + mins;
var obj;
var search;
app.get('/', function(req, res){
res.sendFile(path.join(__dirname + '/index.html'));
});
app.get('/latest', function(req, res){
mongo.connect(url, function(err, db){
if(err) throw err;
var theCollection = db.collection('flickr_image_search');
theCollection.find({}).toArray(function(err, docs){
if(err) throw err;
search = docs;
return docs;
});
db.close();
});
res.send(search);
});
app.get(['/search/:text/:offSet2', '/search/:text'], function(req, res){
var lookInIt = req.params.text;
var listLength = req.params.offSet2;
var searched = flickr.search(lookInIt, listLength || 10);
obj = {
query: lookInIt,
offSet: listLength,
date: today
};
mongo.connect(url, function(err, db){
if(err) throw err;
var imageSearch = db.collection('flickr_image_search');
imageSearch.insert(obj);
db.close();
});
res.send(searched);
});
var port = 3000 || process.env.PORT;
app.listen(port, process.env.IP, function(){
console.log('LISTENING');
});
flickr.js
var private = require('./private');
var Flickr = require("flickrapi"),
flickrOptions = {
api_key: private.key,
secret: private.secret
};
var obj;
function search (query, offSet) {
Flickr.tokenOnly(flickrOptions, function (err, flickr) {
if (err) throw err;
flickr.photos.search({
text: query,
page: 1,
per_page: offSet,
sort: "relevance"
}, function (err, result){
if(err) throw err;
result = result.photos.photo;
for(var key in result){
var picUrl = 'https://farm' + result[key]["farm"] + '.staticflickr.com/' + result[key]["server"] + '/' + result[key].id + '_' + result[key].secret + '.jpg'
result[key].picUrl = picUrl;
var userUrl = 'https://www.flickr.com/photos/' + result[key].owner + '/' + result[key].id;
result[key].userUrl = userUrl;
delete result[key]["ispublic"];
delete result[key]["isfriend"];
delete result[key]['isfamily'];
delete result[key]['id'];
delete result[key]['owner'];
delete result[key]['server'];
delete result[key]['farm'];
delete result[key]['secret'];
}
obj = result;
});
});
return obj;
}
module.exports.search = search;
index.html
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<title>flickr Image Search</title>
<div class='container'>
<h1 class='text-center'>Flickr Image Search</h1>
<h3 class='well'>To get a series of URLs for images, type 'search/whatever-you-want-to-search/the-amount-of-photos-you-want-returned' in the address bar after 'someWebsite.com/'.
If you want to see the most latest searches that were searched, instead of 'search/whatever-you-want-to-search/the-amount-of-photos-you-want-returned' type 'latest'.
When you find the URL that you want, just copy+paste!</h3>
<p class=lead>This application is purely functional, so chances are the queries won't return clean.</p>
<p>This is an application that searches through the photos of Flickr, using its API.</p>
</div>
Heroku 使用 MONGODB_URI
,但你有 MONGOLAB_URI
。
这可以解释为什么字符串有效但环境变量无效。
尝试在 shell 中输入:heroku config
找到 heroku 配置变量。我敢猜测它是 MONGODB_URI
而不是 MONGOLAB_URI
。
如果是这种情况,请重置您的环境和 heroku 配置变量以使用 MONGODB_URI
。
您在评论中说:"I'm not sure how to check it on Heroku, especially since the app doesn't want to run on there. Heroku has been giving me troubles when deploying applications in general."
要检查您的配置变量,您可以在 shell 中键入 heroku config
,或者您可以导航到您的应用仪表板。单击设置。单击显示配置变量。
当您在仪表板中时,您可以通过单击“资源”选项卡来配置 Heroku 提供的 mLab Add-on。然后在 Add-on 的搜索框中,输入 mLab MongoDB。
如果您想知道服务器设置是什么样的,请参阅下面的代码。这将在适当的地方连接到您的本地和 mLab 数据库。
if(process.env.MONGODB_URI) {
mongoose.connect(process.env.MONGODB_URI);
}else {
mongoose.connect(db, function(err){ //db = 'mongodb://localhost/yourdb'
if(err){
console.log(err);
}else {
console.log('mongoose connection is successful on: ' + db);
}
});
}
在声明 process.env 变量之前安装 dotenv (https://www.npmjs.com/package/dotenv)、require('dotenv')、然后安装 dotenv.load() 对我很有帮助。现在我的 .env 文件中的内容被读取到以前没有的地方。谢谢