简体   繁体   English

如何检查对象是否已经创建?

[英]How to check if an object has been already created?

Hello i would like to check if an object was created.您好,我想检查是否创建了对象。 If its created then redirect to the id page but if it isnt created redirect to the page where its being created.如果它被创建,则重定向到 id 页面,但如果它没有被创建,则重定向到它被创建的页面。

Here is my app这是我的应用程序

 app.get('/my',isLoggedIn,async (req,res)=> { const safe= await Safe.find({}); if(safe===undefined){ res.redirect('/mysafe') } else { res.redirect(`mysafe/${safe._id}`) } })

i think it should work but im getting a cast error CastError: Cast to ObjectId failed for value "undefined" (type string) at path "_id" for model "Safe"我认为它应该可以工作,但我收到一个转换错误 CastError: Cast to ObjectId failed for value "undefined" (type string) at path "_id" for model "Safe"

You just need to flip your conditional and use if(safe) as the condition.你只需要翻转你的条件并使用if(safe)作为条件。 That will be true if the object has been created and false if not.如果对象已创建,则为真,否则为假。 Something along the lines of:类似的东西:

 app.get('/my', isLoggedIn, async (req,res) => { const safe= await Safe.find({}); if(safe){ res.redirect(`mysafe/${safe._id}`) } else { res.redirect('/mysafe') } })

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

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