我的使用Mongoose的API有Micro个端点。除了使用.findOne()的代码,其他所有代码都可以正常工作。这将在本地返回正确的数据,但在部署到Now v2时不会返回。这是端点:
const { createError, run, send } = require("micro");
const mongoose = require("mongoose").set("debug", true);
const mongooseConnect = require("./utils/mongooseConnect");
const Art = require("./schemas").art;
mongooseConnect();
const art = async (req, res) => {
console.log("hello world");
const id = req.url.split("id=")[1].split("&")[0];
console.log("id", id);
console.log("mongoose.Types.ObjectId(id)", mongoose.Types.ObjectId(id));
try {
const data = await Art.findOne({ _id: id });
console.log("data", data);
if (!data) {
throw createError(404, "Item of art not found.");
}
send(res, 200, { data });
} catch (error) {
console.log(error);
throw createError(error.statusCode, error.statusText);
}
};
module.exports = (req, res) => run(req, res, art);
这是输出:
2019-05-25T02:07:12.706Z hello world
2019-05-25T02:07:12.706Z id 5cb0a26bea66fa13786dc289
2019-05-25T02:07:12.707Z mongoose.Types.ObjectId(id) 5cb0a26bea66fa13786dc289
2019-05-25T02:07:13.104Z Mongoose: arts.findOne({ _id: ObjectId("5cb0a26bea66fa13786dc289") }, { projection: {} })
2019-05-25T02:07:13.173Z data null
2019-05-25T02:07:13.174Z { Error: Item of art not found.
at createError (/var/task/api/art.js:1584:14)
at art (/var/task/api/art.js:98943:13)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7) statusCode: 404, originalError: undefined }
2019-05-25T02:07:13.175Z Error
at createError (/var/task/api/art.js:1584:14)
at art (/var/task/api/art.js:98949:11)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7)
04 MB 104 MB 104 MB 104 MB 104 MB 104 MB 104 MB 104 MB 104 MB 104 MB 104 MB 104 MB 104 MB 104 MB 104 MB 104 MB 104 MB 104 MB 104 MB 104 MB 104 MB 104 MB 104 MB 104 MB 104 MB 104 MB 104 MB
2019-05-25T02:07:32.932Z running builder.exports.prepareCache...
2019-05-25T02:07:32.933Z preparing cache ...
2019-05-25T02:07:32.937Z producing cache file manifest ...
对我来说,很奇怪的是,我可以在另一个端点(Art
)中看到const data = await Art.paginate();
模式的所有文档。在那里,我看到了应该匹配的文件
{
_id: '5cb0a26bea66fa13786dc289',
title: 'sdfsda'
}
请让我知道是否还有其他细节可以帮助您。