所以我想要返回数据库结果而不是其他。目前我正在返回一大块 JSON 数据(如下所示):
但我只需要 [dataValues] 属性。我不想使用这一点JSON
检索它:tagData[0].dataValues.tagId
.
我刚刚注意到:当它发现并且不创造,它将返回JSON
对于数据库结果,但是当它没有找到并创建,它返回不需要的 JSON blob(如下所示)有没有办法解决这个问题?
[ { dataValues:
{ tagId: 1,
tagName: '#hash',
updatedAt: Fri Dec 25 2015 17:07:13 GMT+1100 (AEDT),
createdAt: Fri Dec 25 2015 17:07:13 GMT+1100 (AEDT) },
_previousDataValues:
{ tagId: 1,
tagName: '#hash',
createdAt: Fri Dec 25 2015 17:07:13 GMT+1100 (AEDT),
updatedAt: Fri Dec 25 2015 17:07:13 GMT+1100 (AEDT) },
_changed:
{ tagId: false,
tagName: false,
createdAt: false,
updatedAt: false },
'$modelOptions':
{ timestamps: true,
instanceMethods: {},
classMethods: {},
validate: {},
freezeTableName: true,
underscored: false,
underscoredAll: false,
paranoid: false,
whereCollection: [Object],
schema: null,
schemaDelimiter: '',
defaultScope: null,
scopes: [],
hooks: {},
indexes: [],
name: [Object],
omitNull: false,
sequelize: [Object],
uniqueKeys: [Object],
hasPrimaryKeys: true },
'$options':
{ isNewRecord: true,
'$schema': null,
'$schemaDelimiter': '',
attributes: undefined,
include: undefined,
raw: true,
silent: undefined },
hasPrimaryKeys: true,
__eagerlyLoadedAssociations: [],
isNewRecord: false },
true ]
我不需要像上面那样得到大斑点,而是只需要RAW
json 结果(如下所示):
{ tagId: 1,
tagName: '#hash',
updatedAt: Fri Dec 25 2015 17:07:13 GMT+1100 (AEDT),
createdAt: Fri Dec 25 2015 17:07:13 GMT+1100 (AEDT) },
我使用了以下 JavaScript。我确实尝试添加raw: true
,但没有成功?
// Find or create new tag (hashtag), then insert it into DB with photoId relation
module.exports = function(tag, photoId) {
tags.findOrCreate( {
where: { tagName: tag },
raw: true
})
.then(function(tagData){
// console.log("----------------> ", tagData[0].dataValues.tagId);
console.log(tagData);
tagsRelation.create({ tagId: tagData[0].dataValues.tagId, photoId: photoId })
.then(function(hashtag){
// console.log("\nHashtag has been inserted into DB: ", hashtag);
}).catch(function(err){
console.log("\nError inserting tags and relation: ", err);
});
}).catch(function(err){
if(err){
console.log(err);
}
});
}
Edit:
所以我调查了一下,看来大JSON
仅当以下情况时才返回 blobSequelize
是创造而不是发现。
有没有办法解决这个问题?
Edit 2:
好吧,我找到了一个解决方法,可以将其变成可重用的函数。但如果有一些内置的东西Sequelize
,我更愿意使用它。
var tagId = "";
// Extract tagId from json blob
if(tagData[0].hasOwnProperty('dataValues')){
console.log("1");
tagId = tagData[0].dataValues.tagId;
} else {
console.log("2");
console.log(tagData);
tagId = tagData[0].tagId;
}
console.log(tagId);
tagsRelation.create({ tagId: tagId, photoId: photoId })
Edit 3:
因此,我认为没有“官方”续集方法可以实现此目的,因此我只是编写了一个自定义模块,该模块返回JSON
所需的数据。该模块可以定制和扩展以适应各种情况!如果有人对如何改进该模块有任何建议,请随时发表评论:)
在此模块中,我们返回一个 Javascript 对象。如果你想把它变成JSON
只需使用它进行字符串化JSON.stringify(data)
.
// Pass in your sequelize JSON object
module.exports = function(json){
var returnedJson = []; // This will be the object we return
json = JSON.parse(json);
// Extract the JSON we need
if(json[0].hasOwnProperty('dataValues')){
console.log("HI: " + json[0].dataValues);
returnedJson = json[0].dataValues; // This must be an INSERT...so dig deeper into the JSON object
} else {
console.log(json[0]);
returnedJson = json[0]; // This is a find...so the JSON exists here
}
return returnedJson; // Finally return the json object so it can be used
}
Edit 4:
于是就有了官方的sequelize方法。请参阅下面已接受的答案。