问题
I have Micro endpoints for my API that uses Mongoose. All of them work fine, except the one that uses .findOne(). This one returns the right data locally, but not when deployed to Now v2. Here is the endpoint:
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);
And here is the output:
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 ...
The weird thing to me is that I can see all documents of the Art
schema in another endpoint (const data = await Art.paginate();
). There, I see the document that should match
{
_id: '5cb0a26bea66fa13786dc289',
title: 'sdfsda'
}
Please let me know if any other details would help.
来源:https://stackoverflow.com/questions/56301259/mongoose-findone-breaks-when-deployed