AngularJS读取带点JSON变量及Sequelize内连接查询问题排查
Hey Miguel-Ángel, let's break down your two technical problems and fix them step by step:
1. Reading JSON Variables with Dots in AngularJS
When your JSON keys include dots (like {"user.profile.name": "Miguel"}), AngularJS's default dot notation ({{data.user.profile.name}}) will try to access nested properties instead of the exact key with dots. Here's how to fix it:
- Use bracket notation in templates: Instead of dot syntax, wrap the key in quotes inside square brackets. For example:
<!-- Correct way to access a key with dots --> <p>{{ yourDataObject['origin.location'] }}</p> - Same logic applies in controllers: If you need to access the value in your AngularJS controller, use the same bracket syntax:
$scope.originLocation = $scope.yourDataObject['origin.location']; - For ng-repeat scenarios: When looping through objects with dotted keys, reference them like this:
<div ng-repeat="(key, value) in yourDataObject"> <span>{{ key }}: {{ value }}</span> <!-- Or if targeting a specific dotted key --> <span ng-if="key === 'failure.equipment'">{{ value }}</span> </div>
This bypasses Angular's nested property lookup and directly targets the exact key string.
2. Sequelize Multiple Inner Joins: Success False But Data Exists
Looking at your response, it's clear there's a mismatch between your success: false flag and the actual data present in the data field. Here's what's likely going wrong and how to fix it:
Common Causes & Fixes
Incorrect success-check logic in your backend
Your code is probably checking for an empty result incorrectly. For example:- If you used
findAll()(returns an array), but checkedresult === nullinstead ofresult.length === 0 - If you used
findOne()(returns an object), but checkedresult.length === 0(which will always beundefined/false)
Fix your response logic to match the Sequelize method:
// Example for findAll() (returns array) const queryResult = await YourModel.findAll({ include: [ { model: ModelB, required: true }, // Required = true enforces INNER JOIN { model: ModelC, required: true }, // Add other joined models here ], where: { codWO: '1016285246' } }); if (queryResult.length === 0) { res.json({ success: false, message: 'Query returned no results', data: {} }); } else { res.json({ success: true, message: 'Query successful', data: queryResult }); }- If you used
Misconfigured Inner Joins
Double-check your Sequelize model associations and join conditions:- Ensure
required: trueis set for each included model (this triggers an INNER JOIN instead of the default LEFT JOIN) - Verify that foreign keys in your models match the database schema (e.g.,
ModelA.belongsTo(ModelB, { foreignKey: 'codType' })) - Log the generated SQL to debug: Add
logging: console.logto your query to see exactly what Sequelize is sending to the database. Run that SQL directly in your database client to confirm if it returns the expected data.
- Ensure
Placeholder Data in Results
The values like1016285246--originlook like placeholder or default values. This might mean your joins are pulling in data from a fallback table, or your model definitions have default values set incorrectly. Check your model schemas to ensure default values aren't overriding actual joined data.
内容的提问来源于stack exchange,提问作者Miguel-Ángel

