在过去的几个月中,我最近开始注意到这一点,但不确定是什么原因。我正在创建要推送到数组的对象。在将所有对象推入具有相同属性的数组之后,我使用JSON.stringify方法将其转换为json字符串,以便可以将该对象数组插入到数据库中。我注意到的问题是,其中一些对象缺少应有的属性。
这是与该问题有关的文件的一部分
AddPartHandler(){
let items = this.state.ItemList;
let part = this.state.ItemSelected;
for(var i = 0; i < items.length; i++){
if(items[i].ItemName == part){
var ItemToPush = {
ID: items[i].ID,
Quantity: 1,
Serial: "No"
}
//console.log(ItemToPush);
listToPush.push(ItemToPush);
this.setState({ListToPush: listToPush});
}
列表完成后,我就准备好了,并像这样发送到我的数据库
AddBuildHandler(){
let partName = this.state.PartName;
let partsArray = this.state.ListToPush;
let array = [];
let todaysDate = new Date();
todaysDate = (todaysDate.getMonth() + 1) + "-" + todaysDate.getDate() + "-" + todaysDate.getFullYear()
let lastId = parseInt(this.state.LastID);
lastId = lastId + 1;
//console.log(JSON.stringify(partsArray));
var ItemObject = {
PartName: this.state.AssemblyName,
Type: "Assembly Item",
Category: "Parts",
Units: "unit",
IsBulk: "FALSE",
Location: this.state.Location,
DateCreated: todaysDate,
CreatedBy: this.state.User,
PartList: JSON.stringify(partsArray),
ReorderNumber: "",
ModelNumber: "",
Price: "",
Description: "",
Serial: "",
}
socket.emit('insertNewBuildItem', ItemObject, (boolean) => {
if(!boolean){
alert('Could not create Assembly Item');
}
});
如果在stringify方法之后执行控制台日志,则会得到类似的内容
{ID: 325, Serial: "No", Quantity: 1}, {ID: 302, Serial: "No", Quantity: 1}
但是有时我的数据库中显示的内容是这样的
{ID: 325, Quantity: 1}, {ID: 302, Serial: "No", Quantity: 1}
这是我的数据库序列,以防万一有人需要
export const insertNewBuildItem = (ItemObject) => {
return new Promise(async (resolve, reject) => {
try {
console.log(JSON.stringify(ItemObject.PartList))
const insertNewBuildItem = `INSERT INTO dispatch_console.inventory_items (ItemName, Type, Category, IsBulk, DateCreated, Createdby, PartNumber, ModelNumber, Description, Serial, Units, ReorderNumber, PartList, Location) VALUES ('${ItemObject.PartName}', '${ItemObject.Type}', '${ItemObject.Category}', '${ItemObject.IsBulk}', '${ItemObject.DateCreated}', '${ItemObject.CreatedBy}', '${ItemObject.PartNumber}', '${ItemObject.ModelNumber}', '${ItemObject.Description}', '${ItemObject.Serial}', '${ItemObject.Units}', '${ItemObject.ReorderNumber}', '${ItemObject.PartList}', '${ItemObject.Location}');`;
const response = await db(insertNewBuildItem, "Inserting New Item to Build List");
resolve(response);
} catch (e) {
console.log("ERROR inventory.insertNewBuildItem: " + e);
reject(e);
}
});
};
我在代码中还有很多其他地方,我做同样的事情,没有任何这些问题,因此,对于是否随机地忽略属性,我有点困惑。如果有人有什么想法或建议可以帮助我一堆。谢谢!