简体   繁体   English

Mongoose没有为api / mlab保存数据

[英]Mongoose not saving data for api/mlab

I have a search api that you can search using the imgur api and it'll return json data of the images, but will also record your latest search in the "/latest" url. 我有一个搜索API,您可以使用imgur api进行搜索,它将返回图像的json数据,但也会在“/ latest”网址中记录您的最新搜索。 The only problem is it returns an empty array which leads me to believe my mlab database isn't saving anything despite having a save method. 唯一的问题是它返回一个空数组,这让我相信我的mlab数据库没有保存任何东西,尽管有一个保存方法。

Whole file: https://github.com/jeffm64/image-search-abstraction-layer 整个文件: https//github.com/jeffm64/image-search-abstraction-layer

the code that may be not allowing it: index.js 可能不允许的代码:index.js

        router.get("/latest", function (req, res) {
          History.find({}, 'term when -_id').sort('-when').limit(10).then(function (results) {
            res.json(results);
          });
        });

        router.get("/search/:q", function (req, res) {
          imgur.getImage(req.params.q, req.query.offset).then(function (ans) {
            new History({ term: req.params.q }).save();
            res.json(ans);
          });
        });

history.js history.js

var historySchema = new mongoose.Schema({  term: String,  when: { type: Date, default: Date.now } });

var History = mongoose.model("History", historySchema);

Ok so the answer to this is super noob level. 好的,所以对此的答案是超级菜鸟水平。 It wouldn't connect to the database because you made the user login for mlabs database read me only on accident. 它不会连接到数据库,因为你让mlabs数据库的用户登录只在意外时读给我。 I guess I will leave this in case some other poor soul spends 15+ hours troubleshooting code that's probably fine! 我想我会留下这个以防万一其他可怜的灵魂花了15个多小时来排除代码,这可能很好!

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

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