问题描述
我正在使用molecularjs 操作将数据输入到我的数据库中。主要是我使用 NodeJS 和 mongoose 来定义模型。到目前为止,我能够创建一个模型,但是当我尝试创建一对多关系时,它给了我这个错误
errors: { 'deliveryParcels.0': CastError: Cast to [ObjectId] 失败 对于值 "[{"parcelId":"LHE660851871415","address":"24 E muhafiz town lahore","type":"COD","addressLocation":{"lat":31.4532941,"lng":74.2166403},"customerName":"ALI MALHI","customerPhone":"3314488396","processingStatus":"pending","amount":2500,"vendorOrderId":"other"}]" 在路径“deliveryParcels.0”
我希望同时创建 rider-tasks
和 delivery-parcels
两个集合,但只创建 rider-tasks
集合而不创建 delivery-parcels
。我的架构做错了什么?
这是我的模型
骑手任务模型
import mongoose,{ Schema } from "mongoose";
import { getDbConnection } from "../../../utils/db/db-connect";
/* eslint-disable id-blacklist */
import { IRiderTaskModel } from "../interfaces/rider-task.interface";
const RiderTaskSchema: Schema<IRiderTaskModel> = new Schema<IRiderTaskModel>(
{
_id: { type: String,required: true,unique: true },riderId: { type: String,required: true },totalAmount: { type: Number,riderName: { type: String,cityId: { type: String,cityName: { type: String,status: { type: String,adminName: { type: String,adminId: { type: String,createdAt: { type: Date,deliveryParcels: [
{
type: Schema.Types.ObjectId,ref: "delivery-parcels",},],{ _id: true }
);
const RiderTask = getDbConnection("deliveryManagement").model<IRiderTaskModel>(
"rider-tasks",RiderTaskSchema
);
export { RiderTask };
递送包裹
import mongoose,{ Schema } from "mongoose";
import { getDbConnection } from "../../../utils/db/db-connect";
import { IDeliveryParcelModel } from "../interfaces/delivery-parcels.interface";
const DeliveryParcelsSchema: Schema<IDeliveryParcelModel> = new Schema<IDeliveryParcelModel>(
{
parcelId: { type: String,address: { type: String,addressLocation: {
lat: { type: Number,lng: { type: Number,customerName: { type: String,customerPhone: { type: String,processingStatus: { type: String,amount: { type: Number,riderTaskId: { type: Schema.Types.String,ref: "rider-tasks" },type: { type: String,enum: ["COD","NONCOD"] },postedStatus: {
status: { type: String },statusKey: { type: String },signature: { type: String },reason: { type: String },checkBoxData: [{ type: String }],adminId: { type: String },adminName: { type: String },}
);
const DeliveryParcels = getDbConnection(
"deliveryManagement"
).model<IDeliveryParcelModel>("delivery-parcels",DeliveryParcelsSchema);
export { DeliveryParcels };
我的服务
private async createdRiderTaskHandler(ctx: Context<IRiderTaskModel>) {
const { params } = ctx;
const finalData: IParcelData[][] = await Promise.all(
params.deliveryParcels.map((parcel) =>
this.broker.call("parcel-data.getParcels",{
id: parcel.parcelId,})
)
);
const wrongParcels: string | any[] = [];
const check = finalData[0].filter(
(data) => data.currentStatus.status === "dispatched"
)[0];
if (check) {
wrongParcels.push(check.parcelId);
const parcel = wrongParcels.join("\r\n");
throw new Error(
'These parcels "' + parcel + '" have already been dispatched'
);
}
const codamount = params.deliveryParcels
.filter((data,i) => data.type === "COD")
.reduce((total,b) => total + b.amount,0);
const customId = "RTD-" + Math.floor(100000 + Math.random() * 900000);
try {
const taskData = omit({
...params,_id: customId,totalAmount: codamount,forceAssignment: false,createdAt: new Date(),});
const data = await RiderTask.create(taskData);
return { message: data };
} catch (error) {
this.logger.error(error);
return error;
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)