对于模型 "wallet" 的路径“_id”中的值 "me",转换为 ObjectId 失败
Cast to ObjectId failed for value "me" at path "_id" for model "wallet"
How are you all, I want to get a user's data in a node. Means all the data of a single user. Like this is one register data and the
other is of payment. Now in this I want to get all the payments of how
much a user has paid. I have tried this. But this error is coming
Maybe I am not able to write the code correctly. So please guide me.
获取用户支付方式
router.get('/me', auth, async (req, res) => {
UserWallet.find()
.select("user", " id user")
.populate("user ", "_id user")
.exec()
.then(result => {
res.json({ result })
})
.catch(error => {
console.error(error.message)
res.status(500).send('Server Error')
})
});
Post method of user payments
router.post(
'/payment/add',
async (req, res) => {
try {
const user = await users.findById(req.user.id).select('-password');
const { bankName, tranferRefCode, amount } = req.body;
const newPayment = new UserWallet({
bankName,
tranferRefCode,
amount,
username: user.username,
avatar: user.avatar,
user: req.user.id
});
await newPayment.save().then(payment => {
res.json({ newPayment: payment })
})
} catch (err) {
console.error(err.message);
res.status(500).send('Server Error');
}
}
);
Schema of user payment
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const walletSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'user'
},
bankName: {
type: String,
},
tranferRefCode: {
type: String,
},
amount: {
type: String,
},
username: {
type: String
},
avatar: {
type: String
},
date: {
type: Date,
default: Date.now
}
})
module.exports = Wallet = mongoose.model('wallet', walletSchema)
Result of payment add
{
"newPayment": {
"_id": "5ec0a61597d09440249a1dcc",
"bankName": "sbi",
"tranferRefCode": "kfjalskdfjkasdfjajs",
"amount": "1000",
"username": "s",
"avatar": "https://gravatar.comxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx?d=mm&r=pg&s=200",
"paymentBy": "5eb2fa29d37d9a08741621c8",
"date": "2020-05-17T02:48:53.662Z",
"__v": 0
}
}
This is 100% worked
router.get('/myPayment/user', auth, async (req, res) => {
UserWallet.find({ paymentBy: req.user.id })
.populate("paymentBy", "_id username email")
.then(myPanment => {
res.json({myPanment})
})
.catch(err => {
console.log(err);
res.json({msg: err})
})
});
This Response Result
{
"myPanment": [
{
"_id": "5ec38e65b4af0b1e4ce9ff95",
"bankName": "PUNB",
"tranferRefCode": "xsxsdsdsdmskdmskdmksdmS",
"amount": "51000",
"username": "satendrasingh",
"avatar": "https://gravatar.com/avatar/xxxxxxxx?d=mm&r=pg&s=200",
"paymentBy": {
"_id": "5eb1562e166a03269c3f2d82",
"username": "xxxxx",
"email": "s@gmail.com"
},
"date": "2020-05-19T07:44:37.870Z",
"__v": 0
},
{
"_id": "5ec38f34dd1b131ac4a3d644",
"bankName": "Singh",
"tranferRefCode": "Singh7536051428",
"amount": "8000",
"username": "satendrasingh",
"avatar": "https://gravatar.com/avatar/xxxxxxxxx?d=mm&r=pg&s=200",
"paymentBy": {
"_id": "5eb1562e166a03269c3f2d82",
"username": "xxx",
"email": "s@gmail.com"
},
"date": "2020-05-19T07:48:04.286Z",
"__v": 0
},
{
"_id": "5ec38f62d12b642de859083a",
"bankName": "ljhjkhkjh",
"tranferRefCode": "jhksjhjkhjk5h3k5jhj53h4j53hk5hj5k345657567",
"amount": "548732",
"username": "satendrasingh",
"avatar": "https://gravatar.com/avatar/xxxxxxxxxx?d=mm&r=pg&s=200",
"paymentBy": {
"_id": "5eb1562e166a03269c3f2d82",
"username": "xxxxxxxx",
"email": "s@gmail.com"
},
"date": "2020-05-19T07:48:50.885Z",
"__v": 0
}
]
}
How are you all, I want to get a user's data in a node. Means all the data of a single user. Like this is one register data and the other is of payment. Now in this I want to get all the payments of how much a user has paid. I have tried this. But this error is coming Maybe I am not able to write the code correctly. So please guide me.
获取用户支付方式
router.get('/me', auth, async (req, res) => {
UserWallet.find()
.select("user", " id user")
.populate("user ", "_id user")
.exec()
.then(result => {
res.json({ result })
})
.catch(error => {
console.error(error.message)
res.status(500).send('Server Error')
})
});
Post method of user payments
router.post(
'/payment/add',
async (req, res) => {
try {
const user = await users.findById(req.user.id).select('-password');
const { bankName, tranferRefCode, amount } = req.body;
const newPayment = new UserWallet({
bankName,
tranferRefCode,
amount,
username: user.username,
avatar: user.avatar,
user: req.user.id
});
await newPayment.save().then(payment => {
res.json({ newPayment: payment })
})
} catch (err) {
console.error(err.message);
res.status(500).send('Server Error');
}
}
);
Schema of user payment
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const walletSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'user'
},
bankName: {
type: String,
},
tranferRefCode: {
type: String,
},
amount: {
type: String,
},
username: {
type: String
},
avatar: {
type: String
},
date: {
type: Date,
default: Date.now
}
})
module.exports = Wallet = mongoose.model('wallet', walletSchema)
Result of payment add
{
"newPayment": {
"_id": "5ec0a61597d09440249a1dcc",
"bankName": "sbi",
"tranferRefCode": "kfjalskdfjkasdfjajs",
"amount": "1000",
"username": "s",
"avatar": "https://gravatar.comxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx?d=mm&r=pg&s=200",
"paymentBy": "5eb2fa29d37d9a08741621c8",
"date": "2020-05-17T02:48:53.662Z",
"__v": 0
}
}
This is 100% worked
router.get('/myPayment/user', auth, async (req, res) => {
UserWallet.find({ paymentBy: req.user.id })
.populate("paymentBy", "_id username email")
.then(myPanment => {
res.json({myPanment})
})
.catch(err => {
console.log(err);
res.json({msg: err})
})
});
This Response Result
{
"myPanment": [
{
"_id": "5ec38e65b4af0b1e4ce9ff95",
"bankName": "PUNB",
"tranferRefCode": "xsxsdsdsdmskdmskdmksdmS",
"amount": "51000",
"username": "satendrasingh",
"avatar": "https://gravatar.com/avatar/xxxxxxxx?d=mm&r=pg&s=200",
"paymentBy": {
"_id": "5eb1562e166a03269c3f2d82",
"username": "xxxxx",
"email": "s@gmail.com"
},
"date": "2020-05-19T07:44:37.870Z",
"__v": 0
},
{
"_id": "5ec38f34dd1b131ac4a3d644",
"bankName": "Singh",
"tranferRefCode": "Singh7536051428",
"amount": "8000",
"username": "satendrasingh",
"avatar": "https://gravatar.com/avatar/xxxxxxxxx?d=mm&r=pg&s=200",
"paymentBy": {
"_id": "5eb1562e166a03269c3f2d82",
"username": "xxx",
"email": "s@gmail.com"
},
"date": "2020-05-19T07:48:04.286Z",
"__v": 0
},
{
"_id": "5ec38f62d12b642de859083a",
"bankName": "ljhjkhkjh",
"tranferRefCode": "jhksjhjkhjk5h3k5jhj53h4j53hk5hj5k345657567",
"amount": "548732",
"username": "satendrasingh",
"avatar": "https://gravatar.com/avatar/xxxxxxxxxx?d=mm&r=pg&s=200",
"paymentBy": {
"_id": "5eb1562e166a03269c3f2d82",
"username": "xxxxxxxx",
"email": "s@gmail.com"
},
"date": "2020-05-19T07:48:50.885Z",
"__v": 0
}
]
}