Node.js上的项目,Express,Jade
以下是获取用户的路线
// Get Single User
router.get('/view/:id', auth.ensureManager, function (req, res) {
...
}
这里面有一些逻辑(2个猫鼬模型:订阅(user_id
字段是唯一的)和订单)
Subscribe.findOne().where({user_id: req.params.id}).exec(function (err, subscribe) {
if (!err) {
Order.find()
.where({user_id: req.params.id})
.sort('-createdAt')
.exec(function (err, orders) {
if (err) {
res.status(500).send();
} else {
res.render('users/view', {
receipt: (subscribe) ? cryptr.decrypt(subscribe.receipt) : null,
orders: orders,
subscribe: subscribe
});
}
});
}
});
收据生成链接(例如 - /subscribes/gil/receipt.jpg
)到存储在public/storage
路径
在玉中我得到这个链接并渲染
if (receipt)
li.list-group.margin-bottom Receipt:
a(href='/storage/' + receipt, target='_blank') view
当我转到此链接时,图片正在打开,但我收到 CastError in console ,如下所示:
{ CastError: Cast to ObjectId failed for value "undefined" at path "_id" for model "Subscribe"
at new CastError (E:\Work\OSPanel\domains\tradecluster-node\node_modules\mongoose\lib\error\cast.js:27:11)
at ObjectId.cast (E:\Work\OSPanel\domains\tradecluster-node\node_modules\mongoose\lib\schema\objectid.js:158:13)
at ObjectId.SchemaType.applySetters (E:\Work\OSPanel\domains\tradecluster-node\node_modules\mongoose\lib\schematype.js:724:12)
at ObjectId.SchemaType._castForQuery (E:\Work\OSPanel\domains\tradecluster-node\node_modules\mongoose\lib\schematype.js:1095:15)
at ObjectId.castForQuery (E:\Work\OSPanel\domains\tradecluster-node\node_modules\mongoose\lib\schema\objectid.js:198:15)
at ObjectId.SchemaType.castForQueryWrapper (E:\Work\OSPanel\domains\tradecluster-node\node_modules\mongoose\lib\schematype.js:1064:15)
at cast (E:\Work\OSPanel\domains\tradecluster-node\node_modules\mongoose\lib\cast.js:300:32)
at model.Query.Query.cast (E:\Work\OSPanel\domains\tradecluster-node\node_modules\mongoose\lib\query.js:3198:12)
at model.Query.Query._castConditions (E:\Work\OSPanel\domains\tradecluster-node\node_modules\mongoose\lib\query.js:1278:10)
at model.Query.Query._findOne (E:\Work\OSPanel\domains\tradecluster-node\node_modules\mongoose\lib\query.js:1494:8)
at model.Query.Query.findOne (E:\Work\OSPanel\domains\tradecluster-node\node_modules\mongoose\lib\query.js:1611:8)
at Function.findOne (E:\Work\OSPanel\domains\tradecluster-node\node_modules\mongoose\lib\model.js:1500:13)
at Function.findById (E:\Work\OSPanel\domains\tradecluster-node\node_modules\mongoose\lib\model.js:1426:15)
at E:\Work\OSPanel\domains\tradecluster-node\routes\users.js:176:15
at Layer.handle [as handle_request] (E:\Work\OSPanel\domains\tradecluster-node\node_modules\express\lib\router\layer.js:95:5)
at next (E:\Work\OSPanel\domains\tradecluster-node\node_modules\express\lib\router\route.js:137:13)
at ensureManager (E:\Work\OSPanel\domains\tradecluster-node\components\auth.js:15:16)
at Layer.handle [as handle_request] (E:\Work\OSPanel\domains\tradecluster-node\node_modules\express\lib\router\layer.js:95:5)
at next (E:\Work\OSPanel\domains\tradecluster-node\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (E:\Work\OSPanel\domains\tradecluster-node\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (E:\Work\OSPanel\domains\tradecluster-node\node_modules\express\lib\router\layer.js:95:5)
at E:\Work\OSPanel\domains\tradecluster-node\node_modules\express\lib\router\index.js:281:22
message: 'Cast to ObjectId failed for value "undefined" at path "_id" for model "Subscribe"',
name: 'CastError',
stringValue: '"undefined"',
kind: 'ObjectId',
value: 'undefined',
path: '_id',
reason: undefined,
model:
{ [Function: model]
hooks: Kareem { _pres: [Object], _posts: [Object] },
base:
Mongoose {
connections: [Array],
models: [Object],
modelSchemas: [Object],
options: [Object],
_pluralize: [Function: pluralize],
plugins: [Array] },
modelName: 'Subscribe',
model: [Function: model],
db:
NativeConnection {
base: [Object],
collections: [Object],
models: [Object],
config: [Object],
replica: false,
hosts: null,
host: 'localhost',
port: 27017,
user: null,
pass: null,
name: 'ldex',
options: null,
otherDbs: [],
relatedDbs: {},
states: [Object],
_readyState: 1,
_closeCalled: false,
_hasOpened: true,
_listening: false,
_connectionOptions: [Object],
'$initialConnection': [Object],
_events: [Object],
_eventsCount: 1,
db: [Object],
client: [Object] },
discriminators: undefined,
'$appliedMethods': true,
'$appliedHooks': true,
schema:
Schema {
obj: [Object],
paths: [Object],
aliases: {},
subpaths: {},
virtuals: [Object],
singleNestedPaths: {},
nested: {},
inherits: {},
callQueue: [],
_indexes: [],
methods: [Object],
statics: {},
tree: [Object],
query: {},
childSchemas: [],
plugins: [Array],
s: [Object],
_userProvidedOptions: [Object],
options: [Object],
'$globalPluginsApplied': true,
_requiredpaths: [Array] },
collection:
NativeCollection {
collection: [Object],
opts: [Object],
name: 'subscribes',
collectionName: 'subscribes',
conn: [Object],
queue: [],
buffer: false,
emitter: [Object] },
Query: { [Function] base: [Object] },
'$__insertMany': [Function],
'$init': Promise { [Circular], catch: [Function] } } }
同时,如果我复制此链接(http://localhost:3000/storage/subscribes/gil/receipt.jpg)并将其直接粘贴到浏览器,则可以点击“在新标签页中打开”'在窗口中的链接上我看到图像和控制台中没有错误。
UPD 对象订阅是
{
"payed": true,
"_id": "5abb6057d1eb870ba056be7c",
"user_id": "5abb6023d1eb870ba056be7b",
"requisites": "Tinkoff, card number 0000 0000 0000 0000",
"interval": 6,
"currency": 3,
"cost": 2500,
"createdAt": "2018-03-28T09:28:55.707Z",
"updatedAt": "2018-03-28T09:49:24.882Z",
"__v": 0,
"receipt": "a735d0dda919f0a9bcf54fd6e41691cfd521eb6547c352c606b2220ad27aa91a2c0a4c3ec47c031967e4fec41425378c",
"paid_till": "2018-09-28T09:49:24.875Z"
}